我正在尝试在右侧的信息框中获取最新版本的详细信息。我试图通过使用jsoup抓取this page从框中检索“ 6.2 (Build 9200) / August 1, 2012; 7 years ago
”。
我有一些代码可以提取盒子中的所有数据,但是我不知道如何提取盒子中的特定部分。
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;
}
}
答案 0 :(得分:0)
您可以查询包含以Software_release_life_cycle
结尾的链接的表行:
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,我发现您需要的行(和仅您需要的行-这是至关重要的细节!-)像这样形成。实际上,elements
仅包含Element
。
最后,您仅提取文本。该代码将打印:
Latest release 6.2 (Build 9200) / August 1, 2012; 7 years ago (2012-08-01)[2]
如果您需要进一步的优化,可以随时substring
。
希望我能帮上忙!