【问题标题】:Jsoup selecting table dataJsoup 选择表数据
【发布时间】:2011-06-11 06:14:38
【问题描述】:

对于我的生活,我无法弄清楚如何使用以“51u1FaI-FHL._SL500_AA300_.jpg”结尾的链接的 jsoup 选择 img src。

我尝试了多种方法,但都没有奏效。有什么帮助吗?

doc1 = Jsoup.connect("http://www.amazon.com/gp/product/B0051HDDO2?ie=UTF8&ref=mas_faad").timeout(20000).get();
Element table = doc1.select("table[class=productImageGrid]").first()
Iterator<Element> ite = table.select("td[height=300]").iterator();

谢谢, 科迪

<table style="text-align: center;" border="0" cellpadding="0" cellspacing="0" width="300"> 
  <tr> 
    <td id="prodImageCell" height="300" width="300" style="padding-bottom: 10px;"><img onclick="if(0 ){ async_openImmersiveView(event);} else {openImmersiveView(event);}" class="prod_image_selector" style="cursor:pointer;" onload="if (typeof uet == 'function') { uet('af'); }" **src="http://ecx.images-amazon.com/images/I/51u1FaI-FHL._SL500_AA300_.jpg"** id="prodImage"/><div id="prodImageCellInner" style="position: relative; height:0px; "><!--Comment for IE as it is empty div--></div></td> 
    <td id="prodVideoClick" style="display:none"></td> 
    <img id="loadingImage" src=http://g-ecx.images-amazon.com/images/G/01/ui/loadIndicators/loading-large_boxed._V192195297_.gif style="position: absolute;  z-index: 200; display:none"> 
 </tr> 
  <tr> 
    <td class="tiny" style="padding-bottom: 5px;">&nbsp;<span id="prodImageCaption" style="color: #666666; font-size: 10px;">Click for larger image and other views</span>&nbsp;</td> 
  </tr> 
 </table> 

【问题讨论】:

    标签: java jsoup


    【解决方案1】:

    @user793728:试试这个:-

    document = Jsoup.connect("http://www.amazon.com/gp/product/B0051HDDO2?ie=UTF8&ref=mas_faad").timeout(20000).get();
    
    Elements elements =document.select(".prod_image_selector");
        for (Element element : elements){
            Attributes imageAttributes=element.attributes();
            for (Attribute attribute: imageAttributes){
                if(attribute.getKey().equals("src")){
                String imageURL=attribute.getValue();
                }
            }
    
        }
    

    【讨论】:

      【解决方案2】:

      这里的问题似乎是亚马逊根据请求 UserAgent 向 jsoup 返回的 HTML 与向浏览器返回的 HTML 不同。

      我将UserAgent设置为已知浏览器,并使用#prodImage ID选择元素,结果OK。

      例如

      Document doc = Jsoup.connect("http://www.amazon.com/gp/product/B0051HDDO2?ie=UTF8&ref=mas_faad")
              .timeout(20000)
              .userAgent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_7) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.91 Safari/534.30")
              .get();
      Element img = doc.select("#prodImage").first();
      System.out.println(img.attr("src"));
      

      返回http://ecx.images-amazon.com/images/I/51u1FaI-FHL._SL500_AA300_.jpg

      为了解决此类问题,我建议输出 doc.html() 并查看检​​索到的解析 HTML,因为它可能与浏览器的查看源 HTML 不同(因为服务器可以返回不同的 HTML,并且查看源在 HTML 被整理并构建到 DOM 之前显示)。

      希望这会有所帮助!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多