如何使用 Jsoup 从以下 html 代码中获取“此文本”?
<h2 class="link title"><a href="myhref.html">this text<img width=10
height=10 src="img.jpg" /><span class="blah">
<span>Other texts</span><span class="sometime">00:00</span></span>
</a></h2>
当我尝试
String s = document.select("h2.title").select("a[href]").first().text();
返回
this textOther texts00:00
我尝试读取 Selector 的 API在 Jsoup 中,但无法弄清楚。
另外,我如何获取类 class="link title blah"
的元素(多个类?)。请原谅我对 Jsoup 和 CSS 都略知一二。
最佳答案
使用 Element#ownText()
而不是 Element#text()
.
String s = document.select("h2.link.title a[href]").first().ownText();
请注意,您可以通过将类名选择器连接在一起来选择具有多个类的元素,如 h2.link.title
这将选择 <h2>
至少两者 link
的元素和 title
类。
关于java - 我如何使用 Jsoup 获取此文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9417295/