我必须使用 Jsoup 从网页中提取一些数据。
我已经很容易地提取了标签中包含的数据,但我仍然需要一些没有标签的数据。
这是 HTML 源代码的示例:
<a id="aId" href="aLink" style="aStyle">
<span id="spanId1">
<b>Caldan Therapeutics</b>
Announces Key Appointments And A Collaboration With
<b>Sygnature Discovery</b>
</span>
<span id="spanId2" style="spanStyle2">
5/17/2016
</span>
</a>
我已经提取了<b>
中包含的数据标签以及日期,但我现在想要的是提取句子 Announces Key Appointments And A Collaboration With
.
如你所见,这句话没有标签。
我能做些什么来提取它?
我已经完成了我的研究,我所能找到的就是如何去除所有标签。
感谢您的帮助!
最佳答案
我找到了该特定需求的答案,我想与将来可能遇到相同问题的任何人分享。
您所能做的就是使用函数 ownText()
,它从元素的子标签中排除文本。
在我们的例子中:
public static void main(String[] args) throws Exception {
Document doc = Jsoup.connect("http://source-url").get();
Elements spanTags = doc.getElementsByTag("span");
for (Element spanTag : spanTags) {
String text = spanTag.ownText();
System.out.println(text);
}
}
关于java - 使用Jsoup从网页中获取没有标签的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37294431/