【问题标题】:Using jSoup, how can I extract text that isn't surrounded by any type of tags?使用 jSoup,我如何提取没有被任何类型的标签包围的文本?
【发布时间】:2013-04-09 20:39:46
【问题描述】:

我这里有这段 HTML 代码:

<hr />
<h3>Academic Recovery and Probation Conference Journal</h3>The Recovery Progress Journal is used to
record and guide conference discussions and to monitor students’ academic, behavioral and social
progress. 
<br />

如您所见,“h3”标题下的文本周围没有任何标签?我正在使用 jSoup 将此信息放入 Android 应用程序中。如果它没有定义它的标签,我将如何仅提取那段文本?

【问题讨论】:

    标签: java android jsoup


    【解决方案1】:

    我认为在没有任何其他解析的情况下单独使用 Jsoup 是不可能的。 Jsoup 在 DOM 中定位元素,然后允许您访问元素的数据。

    您将必须找到包含所需段落的最小元素,在其上运行 .text(),然后自己解析 .text() 的结果。

    【讨论】:

    • 我想会是这样。我必须走很长的路。谢谢!
    【解决方案2】:

    在这里回答(谢天谢地):

    Jsoup - extracting text

    懒人的释义:

    // You need to get Nodes, not Elements
    Document doc = Jsoup.parse(str);
    Element div = doc.select("div").first();
    
    for (Node node : div.childNodes()) {
        System.out.println(
            String.format(
                "%s %s",
                node.getClass().getSimpleName(),
                node.toString()
            )
        );
    }
    

    【讨论】:

      猜你喜欢
      • 2010-12-07
      • 2020-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-18
      • 1970-01-01
      • 2013-01-18
      • 1970-01-01
      相关资源
      最近更新 更多