ウィキペディアの記事からテキストを抽出するために Jsoup を使用しようとしています。
私の考えは、すべての見出しとそれぞれのテキスト段落を単純に抽出することです。
各セクションの特定のテキストのみを取得する方法を理解するのに苦労しています。これが私が持っているものです:
public static void main(String[] args) {
String url = "http://en.wikipedia.org/wiki/Albert_Einstein";
Document doc;
try {
doc = Jsoup.connect(url).get();
doc = Jsoup.parse(doc.toString());
Elements titles = doc.select(".mw-headline");
PrintStream out = new PrintStream(new FileOutputStream("output.txt"));
System.setOut(out);
for(Element h3 : doc.select(".mw-headline"))
{
String title = h3.text();
String titleID = h3.id();
Elements paragraphs = doc.select("p#"+titleID);
//Element nextEle=h3.nextElementSibling();
System.out.println(title);
System.out.println("----------------------------------------");
System.out.println(titleID);
System.out.print("\n");
System.out.println(paragraphs.text());
System.out.print("\n");
}
} catch (IOException e) {
System.out.println("deu merda");
e.printStackTrace();
}
これにより、すべての見出しを抽出できますが、各セクションからテキストを取得してそれに応じて印刷する方法がわかりません。見出しのIDでいいかなと思っていたのですが、サイコロがありません。
助けてくれてありがとう!