こんにちは、ウィキペディアにパーソナリティのページがあり、メイン部分からコード HTML を Java ソースで抽出したいと考えています。
あなたはなにか考えはありますか?
こんにちは、ウィキペディアにパーソナリティのページがあり、メイン部分からコード HTML を Java ソースで抽出したいと考えています。
あなたはなにか考えはありますか?
Jsoup、特にセレクター構文を使用します。
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です。
ウィキペディアには API があります: http://www.mediawiki.org/wiki/API:Main_page
これは、適切にフォーマットされたコンテンツ項目ではなく、HTML ソース コードの STRING (一種の 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();
}