Document doc = Jsoup.parse(new URL("http://en.wikipedia.org/", 10000);
Elements interestingParts = doc.select("div.interestingClass");
//get the combined HTML fragments as a String
String selectedHtmlAsString = interestingParts.html();
//get all the links
Elements links = interestingParts.select("a[href]");
//filter the document to include certain tags only
Whitelist allowedTags = Whitelist.simpleText().addTags("blockquote","code", "p");
Cleaner cleaner = new Cleaner(allowedTags);
Document filteredDoc = cleaner.clean(doc);
它是解析HTML页面和提取所需数据的非常有用的API
# 3 楼答案
请注意,这将返回HTML源代码的字符串(某种blob),而不是格式良好的内容项
我自己用这个——一个我需要的小片段。传入url、任何开始和停止文本或布尔值以获取所有内容
public static String getPage(
String url,
String booleanStart,
String booleanStop,
boolean getAll) throws Exception {
StringBuilder page = new StringBuilder();
URL iso3 = new URL(url);
URLConnection iso3conn = iso3.openConnection();
BufferedReader in = new BufferedReader(
new InputStreamReader(
iso3conn.getInputStream()));
String inputLine;
if (getAll) {
while ((inputLine = in.readLine()) != null) {
page.append(inputLine);
}
} else {
boolean save = false;
while ((inputLine = in.readLine()) != null) {
if (inputLine.contains(booleanStart))
save = true;
if (save)
page.append(inputLine);
if (save && inputLine.contains(booleanStop)) {
break;
}
}
}
in.close();
return page.toString();
}
# 1 楼答案
维基百科有API:http://www.mediawiki.org/wiki/API:Main_page
# 2 楼答案
使用Jsoup,特别是selector syntax
它是解析HTML页面和提取所需数据的非常有用的API
# 3 楼答案
请注意,这将返回HTML源代码的字符串(某种blob),而不是格式良好的内容项
我自己用这个——一个我需要的小片段。传入url、任何开始和停止文本或布尔值以获取所有内容
# 4 楼答案