本文介绍了Jsoup(查找元素)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
帮助解决问题,有必要从Wikipedia中提取一些数据,我将在下图中显示它们:
Help solve the problem, it is necessary to pull some data from Wikipedia, I'll show them in the picture below:
在页面代码中,这些数据在这里:
In the page code, these data are here:
如何获取此数据?为此,可以使用jsoup.
How to get this data? to do this is by using jsoup.
我试图这样做:
System.out.println(doc.select("div.mw-body-content > p ").first().text());
但是问题在于,它不是代码中的第一个
,而第二个是针对某个东西的:
But the problem is that it so happens that this is not the first
in code, and the second is for something:
推荐答案
-
通过其ID(应该是唯一的)获取父级
div
:
Elements parent = doc.select("div#mw-body-content");
获取此元素中的所有p
标签(包括您想要的第二个标签):
Get all p
tags in this element (including the second one you would like to have):
Elements paragraphs = parent.getElementsByTag("p");
取第二个:
Take the second of it:
String text = paragraphs.get(1).text();
这篇关于Jsoup(查找元素)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!