简体   繁体   English

jSoup 将 DIV 标签中的文本提取为字符串

[英]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或使用 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".因此,使用保存在变量“text”中的id“textid”获取div中的文本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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