【问题标题】:@src Xpath for an Image Attribute@src Xpath 用于图像属性
【发布时间】:2015-03-21 18:31:25
【问题描述】:

我正在学习 Scrapy Images Pipeline,我确实可以在 stackoverflow 上的 Scrapy 专家的一些特殊帮助下将图像下载到我的本地文件夹。

但是,Xpath 让我头疼,我无法为不同的代码集找出 xpath 选择器。它在下面给出,我想提取与 data-zoomImage 属性关联的链接。

<div class="imgWrapper">
<img src="http://img1a.flixcart.com/img/thumb-default.jpg"
     class="productImage  current"
     data-imageId="IMAE3RDWTGGCWGHQ"
     data-src="http://img6a.flixcart.com/image/lenovo-400x400.jpeg"
     data-zoomImage="http://img5a.flixcart.com/image/lenovo-1100x1100.jpeg
</div>

我尝试使用以下选择器提取链接,但没有成功。

('//img/@data-zoomImage/@src').extract()

请大家帮帮我吗?

更新:

我可以使用以下选择器下载 src 属性

('//img[@class="productImage  current"]/@src').extract()

我可以通过将 @src 更改为 @data-src 来下载 data-src 属性。但我无法通过相同的逻辑选择 data-zoomImage 属性。关于为什么会发生这种情况的任何想法?

【问题讨论】:

  • 我推荐阅读这个 XPath 教程:zvon.org/comp/r/tut-XPath_1.html -- 在抓取时,深入研究 XPath 确实是有回报的。 :)
  • 谢谢。真的很有帮助。

标签: python xpath web-scraping scrapy


【解决方案1】:

有时这可能会有所帮助..

     In [26]: t = """ <div class="imgWrapper">
             <img src="http://img1a.flixcart.com/img/thumb-default.jpg"
             class="productImage  current"
             data-imageId="IMAE3RDWTGGCWGHQ"
             data-src="http://img6a.flixcart.com/image/lenovo-400x400.jpeg"
             data-zoomImage="http://img5a.flixcart.com/image/lenovo-1100x1100.jpeg
             </div> """

    In [27]: from scrapy.selector import Selector

    In [28]: sel.xpath('//img[@class="productImage  current"]/@data-zoomimage').extract()
    Out[28]: [u'http://img5a.flixcart.com/image/lenovo-1100x1100.jpeg\n</div>']

    In [29]: sel.xpath('//img[@class="productImage  current"]/@data-zoomImage').extract()
    Out[29]: []

不知道为什么属性名称从data-zoomImage变成data-zoomimage

【讨论】:

    猜你喜欢
    • 2013-08-20
    • 1970-01-01
    • 2013-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-26
    • 2011-06-08
    • 1970-01-01
    相关资源
    最近更新 更多