使用Jsoup获取内容的范围

时间:2018-07-24 15:25:51

标签: java jsoup

我有这个html

<span style="font-size:20px">
    <span style="font-family:verdana">Text 1</span>
</span>
<span style="font-size:11px">
    <span style="fontfamily:arial">Text 2</span>
</span>

我想获取具有内容的span元素

<span style="fontfamily:arial">Text 2</span>
<span style="font-family:verdana">Text 1</span>

我正在使用JSoup,这是我的尝试

doc.select("span[text!=\"\"]")
doc.select("span[text]")
doc.select("span[content]")
doc.getElementsByTag("span").stream().filter(p -> p.hasText())

可以帮助我吗?

预先感谢

1 个答案:

答案 0 :(得分:1)

Actually, if you want to select spans which should have text between that span, you may easily filter all the spans by using: Element.ownText​() is not empty. See: https://jsoup.org/apidocs/org/jsoup/nodes/Element.html#ownText-- public String ownText​() Gets the text owned by this element only; does not get the combined text of all children.