从维基百科信息框获取具体信息
Get specific information from Wikipedia Information Box
我正在尝试在右侧的信息框中获取最新版本的详细信息。我正在尝试通过使用 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;
}
}
您可以查询包含 link 且 以 Software_release_life_cycle
结束 的 table 行:
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
。
希望我有所帮助!
我正在尝试在右侧的信息框中获取最新版本的详细信息。我正在尝试通过使用 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;
}
}
您可以查询包含 link 且 以 Software_release_life_cycle
结束 的 table 行:
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
。
希望我有所帮助!