我有这个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())
可以帮助我吗?
预先感谢
答案 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.