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

tha*_*you 2 java android text jsoup

我想从网站中提取一些文本并存储在字符串中。

<div class="textclass" id="textid" itemprop="itemtext">I want to get this Text</div>
Run Code Online (Sandbox Code Playgroud)

问号里有什么?

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;
        }
Run Code Online (Sandbox Code Playgroud)

Rag*_*dan 5

使用以下

Elements text = document.select("div");
String desc = text.text();
Log.i(".........",+desc);
Run Code Online (Sandbox Code Playgroud)

尝试结束后的日志

01-31 04:45:15.272: I/.........(1233): I want to get this Text
Run Code Online (Sandbox Code Playgroud)

编辑:

您可以使用

Elements text = document.select("div[class=textclass]");
Run Code Online (Sandbox Code Playgroud)

或使用 id

Elements text = document.select("div[id=textid]");
Run Code Online (Sandbox Code Playgroud)

或者

Elements text = document.select("div[itemprop=itemtext]");
Run Code Online (Sandbox Code Playgroud)