我有以下HTML代码:
Data
<div class="alg"></div>
<div class="alg"></div>
Pepsi
791
<div class="alg"></div>
<div class="alg"></div>
Coke
700
<div class="gap"></div>
<div class="gap"></div>
我想提取所有值Coke,700,pepsi,791。我使用Jsoup尝试了以下代码:
Document doc = Jsoup.parse(html);
for( Element element : doc.select("div.alg") ) // Select all the div tags
{
TextNode next = (TextNode) element.nextSibling(); // Get the next node of each div as a TextNode
System.out.println(next.text()); // Print the text of the TextNode
}
但上面的代码总是打印&#34;&#34;空字符串。
答案 0 :(得分:1)
试试这个:
Document doc = Jsoup.parse(url, 30000);
for( Element element : doc.select(".gap") ) { // Select all the div tags
Node next = element.nextSibling();
StringBuffer sb = new StringBuffer();
while (next instanceof TextNode) {
sb.append(((TextNode)next).text());
next = next.nextSibling();
}
System.out.println(sb.toString()); // Print the text of the TextNode
}