thankyou
thankyou

Reputation: 221

jSoup extract Text out of DIV tag to String

I want to extract some Text out of a website and store in String.

<div class="textclass" id="textid" itemprop="itemtext">I want to get this Text</div>

What goes into the question marks?

protected Void doInBackground(Void... params) {
            try {
                Document document = Jsoup.connect(url).get();

                Elements text = document.select("???");

                desc = text.attr("???");
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

Upvotes: 3

Views: 9803

Answers (2)

Raghunandan
Raghunandan

Reputation: 133560

Use the below

Elements text = document.select("div");
String desc = text.text();
Log.i(".........",+desc);

The log after trying at my end

01-31 04:45:15.272: I/.........(1233): I want to get this Text

Edit:

You can use

Elements text = document.select("div[class=textclass]");

or using id

Elements text = document.select("div[id=textid]");

or

Elements text = document.select("div[itemprop=itemtext]");

Upvotes: 7

Enzo
Enzo

Reputation: 7

You can try this:

    Document doc1 = Jsoup.connect(url).get();
    Element contentDiv = doc1.select("div[id=textid]").first();
    String text=contentDiv.getElementsByTag("div").text();

    System.out.println(text); // The result

So get the text in the div with the id "textid" saved in the variable "text".

Upvotes: 0

Related Questions