我正在尝试在右侧的信息框中获取最新版本的详细信息。我正在尝试通过使用jsoup抓取6.2 (Build 9200) / August 1, 2012; 7 years ago
从框中检索“ this page”。
我有代码可以从包装盒中提取所有数据,但我不知道如何提取包装盒的特定部分。
org.jsoup.Connection.Response res = Jsoup.connect("https://en.wikipedia.org/wiki/Windows_Server_2012").execute();
String html = res.body();
Document doc2 = Jsoup.parseBodyFragment(html);
Element body = doc2.body();
Elements tables = body.getElementsByTag("table");
for (Element table : tables) {
if (table.className().contains("infobox")==true) {
System.out.println(table.outerHtml());
break;
}
}
您可以查询包含Software_release_life_cycle
为ends的链接的表行:
String url = "https://en.wikipedia.org/wiki/Windows_Server_2012";
try {
Document document = Jsoup.connect(url).get();
Elements elements = document.select("tr:has([href$=Software_release_life_cycle])");
for (Element element: elements){
System.out.println(element.text());
}
}
catch (IOException e) {
//exception handling
}
这就是为什么,通过查看完整的html,我发现所需的行(和only所需的行-这是至关重要的细节!-)是这样形成的。实际上,elements
实际上仅包含一个Element
。
最后只提取文本。此代码将打印:
Latest release 6.2 (Build 9200) / August 1, 2012; 7 years ago (2012-08-01)[2]
如果需要更多的改进,您可以随时将其substring
。
希望我帮忙!