[英]jSoup extract Text out of DIV tag to String
我想從網站中提取一些文本並存儲在字符串中。
<div class="textclass" id="textid" itemprop="itemtext">I want to get this Text</div>
問號里有什么?
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;
}
使用以下
Elements text = document.select("div");
String desc = text.text();
Log.i(".........",+desc);
嘗試結束后的日志
01-31 04:45:15.272: I/.........(1233): I want to get this Text
編輯:
您可以使用
Elements text = document.select("div[class=textclass]");
或使用 id
Elements text = document.select("div[id=textid]");
或者
Elements text = document.select("div[itemprop=itemtext]");
你可以試試這個:
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
因此,使用保存在變量“text”中的id“textid”獲取div中的文本。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.