Get data from a JSOUP div class

I need to get the value "8.32" from "rnicper", "36 mg" from "rnstr" and "20/80 PG / VG" from "nirat".

<div class="recline highlight" id="rnic">
          <div class="rlab"><span class="nopr indic indic-danger"></span>Nicotine juice <span id="rnstr">36 mg</span> (<span id="nirat">20/80 PG/VG</span>)</div>
          <div class="runit" id="rnicml">2.08</div>
          <div class="rdrops" id="rnicdr">73</div>
          <div class="rgrams" id="rnicg" style="display: none;">2.53</div>
          <div class="rpercent" id="rnicper">8.32</div><br>
        </div>

I tried various methods, but nothing happens.

doc.getElementById("rnicper").outerHtml();
doc.getElementById("rnicper").text();
doc.select("div#rnicper");
doc.getElementsByAttributeValue("id", "rnicper").text();

Please tell me how can I get this information using JSOUP?

Update for Chintak Patel

AsyncTask asyncTask = new AsyncTask() {
            @Override
            protected Object doInBackground(Object[] objects) {
                Document doc = null;
                try {
                    doc = Jsoup.connect("http://e-liquid-recipes.com/recipe/2254223/RY4D%20Vanilla%20Swirl%20DL").get();
                } catch (IOException e) {
                    e.printStackTrace();
                }

                String content =  doc.select("div[id=rnicper]").text();
                Log.d("content", content);

                return null;
            }
        };
        asyncTask.execute();
+4
source share
2 answers

The parameter values ​​you are trying to get are not part of the original html, but are set by javascript after the page loads. Jsoup gets static html, doesn't execute javascript code.

To get what you want, you can use a tool like HtmlUnit or Selenium.

HtmlUnit example:

    try (final WebClient webClient = new WebClient()) {
        webClient.getOptions().setThrowExceptionOnScriptError(false);
        final HtmlPage page = webClient
                .getPage("http://e-liquid-recipes.com/recipe/2254223/RY4D%20Vanilla%20Swirl%20DL");

        System.out.println(page.getElementById("rnicper").asText());

    }
+2
source

Activity JSoup. - . URL- div[id=rnicper] select(). postExecute().

private class GetVersionCode extends AsyncTask<Void, String, String> {
    @Override
    protected String doInBackground(Void... voids) {

        String newVersion = null;
        try {
            newVersion = Jsoup.connect("https://play.google.com/store/apps/details?id=" + MainActivity.this.getPackageName() + "&hl=en")
                    .timeout(30000)
                    .userAgent("Mozilla/5.0 (Windows; U; WindowsNT 5.1; en-US; rv1.8.1.6) Gecko/20070725 Firefox/2.0.0.6")
                    .referrer("http://www.google.com")
                    .get()
                    .select("div[itemprop=softwareVersion]")
                    .first()
                    .ownText();
            return newVersion;
        } catch (Exception e) {
            return newVersion;
        }
    }

    @Override
    protected void onPostExecute(String onlineVersion) {
        super.onPostExecute(onlineVersion);
        if (onlineVersion != null && !onlineVersion.isEmpty()) {
            if (Float.valueOf(currentVersion) < Float.valueOf(onlineVersion)) {
                showAlertDialogForUpdate(currentVersion, onlineVersion);
            }
        }
        Log.e("update", "Current version " + currentVersion + "playstore version " + onlineVersion);
    }
}
0

Source: https://habr.com/ru/post/1694460/


All Articles