简体   繁体   中英

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

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]");

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".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM