只是好奇这在 jsoup 中是否可行
选择一个元素及其文本,但取消选择指定的子元素。
<div>
<p><strong>Location: </strong> Earth</p>
</div>
是否可以只返回地球
?
编辑:此外,在该段落中,位置是静态的,但地球不是。 即你可以拥有
Location: Earth
Location: LA
Location: Mars
最佳答案
您想要使用 Element::ownText
方法。
String html = "<div>\n" + //
"<p><strong>Location: </strong> Earth</p>\n" + //
"</div>";
Document doc = Jsoup.parse(html);
Element p = doc.select("p").first();
if (p!=null) {
System.out.println(p.ownText());
}
输出
Earth
关于css-selectors - 选择元素但取消选择元素的指定子元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35838748/