找不到jsoup(Java)的amazon元素,因为我对Web开发知之甚少

时间:2013-01-28 11:10:38

标签: java html web-scraping amazon jsoup

我目前正在尝试为亚马逊搜索大量数据。我正在使用jsoup来帮助我做到这一点,而且一切都很顺利,但由于某种原因,我无法弄清楚如何拉出目前销售新产品的卖家数量。

以下是我正在抓取的网址示例:http://www.amazon.com/dp/B006L7KIWG

我想从以下内容中提取“39 new”:

<div id="secondaryUsedAndNew" class="mbcOlp">
    <div class="mbcOlpLink">
        <a class="buyAction" href="/gp/offer-listing/B006L7KIWG/ref=dp_olp_new_mbc?ie=UTF8&condition=new">
            39&nbsp;new
        </a> &nbsp;from&nbsp;
        <span class="price">$60.00</span>
     </div>
</div>

这个项目是我第一次使用jsoup,所以编码可能有些不确定,但这里有一些我尝试过的东西:

 String asinPage = "http://www.amazon.com/dp/" + getAsin();
            try {
                Document document = Jsoup.connect(asinPage).timeout(timeout).get();
.....  

  //get new sellers try one
                    Elements links = document.select("a[href]");
                    for (Element link : links) {
                       // System.out.println("Span olp:"+link.text());
                        String code = link.attr("abs:href");
                        String label = trim(link.text(), 35);
                        if (label.contains("new")) {
                            System.out.println(label + " : " + code);
                        }
                    }

    //get new sellers try one
                    Elements links = document.select("div.mbcOlpLink");
                    for (Element link : links) {
                       // System.out.println("Span olp:"+link.text());
                    }

    //about a million other failed attempts that you'll just have to take my word on.

当我在页面上刮掉我需要的所有其他内容时,我已经成功了,但由于某种原因,这个特殊元素很痛苦,任何帮助都会很棒!谢谢你们!

1 个答案:

答案 0 :(得分:0)

我会用

String s = document.select("div[id=secondaryUsedAndNew] a.buyAction").text.replace("&nbsp;"," ");

这应该会让你在此时在页面上显示“42 new”。

希望这适合你!