0

Jsoup を使用して、buy.com ページからレビューと評価情報を抽出したいと考えています。問題は、すべてのレビューの ID がその番号によって異なるため、その方法を理解できないように見えることです。たとえば、レビュー番号 11 は次のようになります。

<a id="CustomerReviews_customerReviews_ctl11_reviewIdAnchor" name="a352496">&nbsp;</a><br />

<span id="CustomerReviews_customerReviews_ctl11_ratingInfo"><span class="blueText"><b>5</b> of <b>5</b></span> <b>Great Product</b> 12/15/2010<br /></span>

<span id="CustomerReviews_customerReviews_ctl11_reviewerInfo"><b>A customer</b> from x<br></span>

<span id="CustomerReviews_customerReviews_ctl11_reviewContent">content</span>

レビュー番号 12 の ID は ctl12 になりますが、ページ内のすべてのレビューのレビュー コンテンツと評価を抽出するにはどうすればよいですか?

4

1 に答える 1

1

少し遅れましたが、同じ問題を見つける可能性のあるあなたや他の人に役立つことを願っています!

次のようなことを試してみてください。

String code1 = "<span id=\"CustomerReviews_customerReviews_ctl11_ratingInfo\"><span class=\"blueText\"><b>1</b> of <b>5</b></span> <b>Great Product</b> 12/15/2010<br /></span>";
String code2 = "<span id=\"CustomerReviews_customerReviews_ctl12_ratingInfo\"><span class=\"blueText\"><b>2</b> of <b>5</b></span> <b>Bad product</b> 12/03/2010<br /></span>";

Document document = Jsoup.parse(code1 + code2);

Elements elements = document.select("span[id~=CustomerReviews_customerReviews_ctl.*_ratingInfo] ");

for (Element element : elements) {
    System.out.println(element.outerHtml());
        Elements spanBlueText = element.select("span > span > b");
        String note = spanBlueText.get(0).text();
        String max = spanBlueText.get(1).text();
        System.out.println("    - note: " + note + "/" + max);

        String comment = element.select("> b").text();
        System.out.println("    - comment: " + comment);

        String date = element.text();
        date = date.substring(date.length() - 10);
        System.out.println("    - date: " + date);
}

selectこの例では、Jsoupメソッドを多用しています。Jsoup Cookbookで引数の正しい構文を見つけることができます。

于 2011-05-05T07:32:19.310 に答える