无法使用XPath获取Image src链接

时间:2015-12-11 12:19:07

标签: html xpath css-selectors scrapy scrapy-shell

我正在使用Scrapy抓取此网站的产品图片src链接:

http://eshop.tesco.com.my/en-GB/Promotion/List?SortBy=Default

由于某些原因,Xpath并没有抓住产品图片src链接。我试图通过使用此Xpath在Scrapy Shell中测试来从站点抓取所有图像src链接:

response.xpath('//img').extract()

返回的结果显示,所有产品的src标记中都没有img个链接。

 [u'<img alt="Grocery Home" class="tLogoMain" src="http://assets.ap-tescoassets.com/UIAssets/MY/grocery/default/i368/tLogoMain.gif" title="Grocery Home">',
 u'<img src="http://assets.ap-tescoassets.com/UIAssets/MY/grocery/default/i368/searchFor.png" alt="Search" class="searchFor">',
 u'<img alt="Previous" src="http://assets.ap-tescoassets.com/UIAssets/MY/grocery/default/i368/pg-prev-disbl-btn.png">',
 u'<img alt="Next" src="http://assets.ap-tescoassets.com/UIAssets/MY/grocery/default/i368/pg-nxt-btn.png">',
 u'<img alt="Grid view" class="grdView" src="http://assets.ap-tescoassets.com/UIAssets/MY/grocery/default/i368/high-grd-view.png">',
 u'<img alt="List view" class="lstView" src="http://assets.ap-tescoassets.com/UIAssets/MY/grocery/default/i368/unhigh-lst-view.png">',
 u'<img alt="" id="productImg-7072093609">',
 u'<img alt="" id="productImg-7070005656">',
 u'<img alt="" id="productImg-7070005648">',
 u'<img alt="" id="productImg-7000034983">',
 u'<img alt="" id="productImg-7070483892">',
 u'<img alt="" id="productImg-7000035009">',
 u'<img alt="" id="productImg-7000801798">',
 u'<img alt="" id="productImg-7072123710">',
 u'<img alt="" id="productImg-7072123737">',
 u'<img alt="" id="productImg-7072123702">',
 u'<img alt="" id="productImg-7004102002">',
 u'<img alt="" id="productImg-7001314416">',
 u'<img alt="" id="productImg-7001829106">',
 u'<img alt="" id="productImg-7001495593">',
 u'<img alt="" id="productImg-7001812165">',
 u'<img alt="" id="productImg-7001813226">',
 u'<img alt="" id="productImg-7002760339">',
 u'<img alt="" id="productImg-7001812157">',
 u'<img alt="" id="productImg-7002800969">',
 u'<img alt="" id="productImg-7002764067">',
 u'<img alt="" id="productImg-7001866206">',
 u'<img alt="" id="productImg-7070980683">',
 u'<img alt="" id="productImg-7072086912">',
 u'<img alt="" id="productImg-7001884344">',
 u'<img alt="Previous" src="http://assets.ap-tescoassets.com/UIAssets/MY/grocery/default/i368/pg-prev-disbl-btn.png">',
 u'<img alt="Next" src="http://assets.ap-tescoassets.com/UIAssets/MY/grocery/default/i368/pg-nxt-btn.png">',
 u'<img src="http://assets.ap-tescoassets.com/UIAssets/MY/grocery/default/en-GB/i368/btn-bookslot-bskt-d.gif" class="delSlotBtn" alt="Book slot disabled">',
 u'<img src="http://assets.ap-tescoassets.com/UIAssets/MY/grocery/default/en-GB/i368/btn-checkout-bskt-d.gif" class="chkOutBtn" alt="Checkout disabled">',
 u'<img alt="" class="legendImg" src="http://assets.ap-tescoassets.com/UIAssets/MY/grocery/default/en-GB/i368/star.png" title="">',
 u'<img alt="" class="legendImg" src="http://assets.ap-tescoassets.com/UIAssets/MY/grocery/default/en-GB/i368/star.png" title="">',
 u'<img alt="Opens in a new window" src="http://assets.ap-tescoassets.com/UIAssets/MY/grocery/default/en-GB/i368/open-window.png" title="Opens in a new window">',
 u'<img src="http://assets.ap-tescoassets.com/UIAssets/MY/grocery/default/en-GB/i368/btn-fulltrolley-bskt-d.gif" class="fullTrolleyBtn" alt="">',
 u'<img alt="Add to list" class="slAddToListDsbld" src="http://assets.ap-tescoassets.com/UIAssets/MY/grocery/default/i368/dsbld_sl_addtolst_icn.png">',
 u'<img alt="Tesco Strapline" src="http://assets.ap-tescoassets.com/UIAssets/MY/grocery/default/en-GB/i368/footer/strapline_footer_bottom_my.png" title="Tesco Strapline">']

我使用Chrome Inspector再次检查,每个产品都有src链接。为什么返回的结果中没有src链接?

请帮忙。

感谢。

2 个答案:

答案 0 :(得分:0)

这是因为javascript呈现,您访问的网站的纯文本不包含该信息,但在加载过程中,它由javascript脚本填充。

您也可以检查是否在浏览器上安装了一些Toggle Javascript extension,这样您就可以在没有javascript的情况下查看实际下载的内容。

答案 1 :(得分:0)

可能是因为它使用xpath获得了多个节点 - &#39; // img&#39;。

尝试使用以下xpath获取特定节点: .//img [contains(src,&#39; {src的特定值}}&#39;)]