如何使用jsoup提取html标签之外的文本?

时间:2016-11-03 04:40:18

标签: html jsoup

我有以下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;空字符串。

1 个答案:

答案 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
}
相关问题