简体   繁体   English

使用Jsoup获取内容的范围

[英]Get the span with content using Jsoup

I have this html 我有这个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>

I want to get span elements that have content 我想获取具有内容的span元素

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

I'm using JSoup and here are my tries 我正在使用JSoup,这是我的尝试

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

Coul anyone helps me? 有人可以帮助我吗?

Thanks in advance 提前致谢

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. 实际上,如果要选择跨度应在跨度之间包含文本的跨度,则可以使用以下元素轻松过滤所有跨度:Element.ownText()不为空。 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. 参见: https : public String ownText​() Gets the text owned by this element only; does not get the combined text of all children. public String ownText​() Gets the text owned by this element only; does not get the combined text of all children.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM