【问题标题】:scrapy: xpath not returning the full url for @hrefscrapy:xpath 未返回 @href 的完整 url
【发布时间】:2016-10-25 22:34:34
【问题描述】:

使用带有scrapy的xpath执行抓取我没有得到完整的URL

这是我正在查看的网址

使用scrapy shell

scrapy shell "http://www.ybracing.com/omp-ia01854-omp-first-evo-race-suit.html"

我从 shell 中执行以下 xpath 选择

sel.xpath("//*[@id='Thumbnail-Image-Container']/li[1]/a//@href")

只得到一半的href

[<Selector xpath="//*[@id='Thumbnail-Image-Container']/li[1]/a//@href" data=u'http://images.esellerpro.com/2489/I/160/'>]

这是我在浏览器中查看的 html 的 sn-p

        <li><a data-medimg="http://images.esellerpro.com/2489/I/160/260/1/medIA01854-GALLERY.jpg" href="http://images.esellerpro.com/2489/I/160/260/1/lrgIA01854-GALLERY.jpg" class="cloud-zoom-gallery Selected" title="OMP FIRST EVO RACE SUIT" rel="useZoom: 'MainIMGLink', smallImage: 'http://images.esellerpro.com/2489/I/160/260/1/lrgIA01854-GALLERY.jpg'"><img src="http://images.esellerpro.com/2489/I/160/260/1/smIA01854-GALLERY.jpg" alt="OMP FIRST EVO RACE SUIT Thumbnail 1"></a></li>            

这是来自 wget

<li><a data-medimg="http://images.esellerpro.com/2489/I/513/0/medIA01838_GALLERY.JPG" href="http://images.esellerpro.com/2489/I/513/0/lrgIA01838_GALLERY.JPG" class="cloud-zoom-gallery Selected" title="OMP DYNAMO RACE SUIT" rel="useZoom: 'MainIMGLink', smallImage: 'http://images.esellerpro.com/2489/I/513/0/lrgIA01838_GALLERY.JPG'"><img src="http://images.esellerpro.com/2489/I/513/0/smIA01838_GALLERY.JPG" alt="OMP DYNAMO RACE SUIT Thumbnail 1" /></a></li>            

我尝试改变我的 xpath 以获取相同的结果,但仍然得到相同的结果

是什么导致了这个问题,我可以做些什么来解决它想要理解而不是有人只是为我纠正我的 xpath

关于页面本身的一些想法我禁用了 javascript 以查看 js 是否生成了一半的 url,但事实并非如此。我还用 wget 下载了页面,以确认原始 html 中的 url 是完整的

我还没有测试任何其他构建,但我在 centos 7 中使用了 scrapy 1.2.1 和 2.7

我用谷歌搜索,只找到由于 javascript 动态生成数据而无法获取数据的人,但我的数据在 html 中

【问题讨论】:

    标签: python-2.7 xpath web-scraping scrapy


    【解决方案1】:

    通过使用

    sel.xpath("//*[@id='Thumbnail-Image-Container']/li[1]/a//@href")
    

    您将获得Selector 实例的列表,其中data 字段仅显示其所有内容的前几个字节(因为它可能很长)。

    要将内容检索为字符串(而不是Selector 实例),您需要使用.extract.extract_first 之类的内容:

    >>> print(sel.xpath("//*[@id='Thumbnail-Image-Container']/li[1]/a//@href").extract_first())
    http://images.esellerpro.com/2489/I/160/260/1/lrgIA01854-GALLERY.jpg
    

    【讨论】:

    • 谢谢你完美的解释
    猜你喜欢
    • 1970-01-01
    • 2014-12-12
    • 1970-01-01
    • 1970-01-01
    • 2015-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-18
    相关资源
    最近更新 更多