lida
lida

Reputation: 41

Get data from div class by JSOUP

I need to get the value "8.32" from the "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();

Tell me, please, 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();

Upvotes: 4

Views: 490

Answers (2)

Luk
Luk

Reputation: 2246

The values of parameters you are trying to get are are not part of initial html, but are set by javascript after page is loaded. Jsoup only gets static html, does not execute javascript code.

To get what you want you can use 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());

    }

Upvotes: 3

Chintak Patel
Chintak Patel

Reputation: 768

Write the following class in your Activity class and do your execution using JSoup. This code is used to get current version from play store website. you can change the URL and div[id=rnicper] into select() method. and then do your execution in postExecute() method.

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);
    }
}

Upvotes: 0

Related Questions