1

JSoupを使用してhttp://dictionary.reference.com/browse/quickからコンテンツを取得しようとしています。そのページに行くと、彼らがデータを整理する方法は、クイックという単語の各「単語タイプ」(形容詞、動詞、名詞)を独自のセクションとして提示することであり、各セクションには1つ以上のリストが含まれていることがわかります定義。

物事をもう少し複雑にするために、各定義の各単語はさらに別の Dictionary.com ページへのリンクになっています。

quick
    adjective
        1. done, proceeding, or occurring with promptness or rapidity...
        2. that is over or completed within a short interval of time
        ...
        14. Archaic.
            a. endowed with life
            b. having a high degree of vigor, energy, ...
    noun
        1. living persons; the quick and the dead
        2. the tender, sensitive flesh of the living body...
        ...
    adverb
        ...

私がやりたいことは、次のように JSoup を使用して、単語の種類とそれぞれの定義を文字列のリストとして取得することです。

public class Metadata {
    // Ex: "adjective", "noun", etc.
    private String wordType;

    // Ex: String #1: "1. done, proceeding, or occurring with promptness or rapidity..."
    //     String #2: "that is over or completed within a short interval of time..."
    private List<String> definitions;
}

したがって、ページは実際には で構成され、List<Metadata>Metadata要素は 1 つ以上の定義とペアになった単語タイプです。

非常に簡単な API 呼び出しを使用して、単語の種類のリストを見つけることができました。

// Contains 1 Element for each word type, like "adjective", "noun", etc.
Document doc = Jsoup.connect("http://dictionary.reference.com/browse/quick").get();
Elements wordTypes = doc.select("div.body div.pbk span.pg");

doc.select(...)しかし、各Metadataインスタンスを取得するために他に何が必要かを理解するのに苦労しています。

4

2 に答える 2

0

ほらね。ところで、CSS セレクターをテストするには、Chrome デベロッパー ツールでコンソールを有効にして、サイトで直接次のようなクエリをテストすることができます。jQuery('div.body div.pbk div.luna-Ent > .dndata')

Document doc = Jsoup.connect("http://dictionary.reference.com/browse/quick").get();
Elements wordTypes = doc.select("div.body div.pbk");

for (Element wordType : wordTypes) {
    Elements typeOfSpeech = wordType.select("span.pg");

    System.out.println("typeOfSpeech: " + typeOfSpeech.text());

    Elements elements = wordType.select("div.luna-Ent > .dndata");

    for (int i = 0; i < elements.size(); i++) {
        Element element = elements.get(i);
        System.out.println((i + 1) + ". " + element.text());
    }
}
于 2013-11-13T23:32:00.590 に答える