【问题标题】:Scrapy Splash returns empty listScrapy Splash 返回空列表
【发布时间】:2019-06-26 07:00:51
【问题描述】:

我正在尝试用 Scrapy 和 Splash 抓取衣服的图像和一些产品信息。 我想只用产品(所以没有模型)来获取图像。 Such as this image 来自https://www2.hm.com/nl_nl/productpage.0220094001.html

但是,如果我尝试使用

在 Scrapy shell 中获取 src

response.xpath('//figure[contains(@class, "secondary")]//img//@src').extract()

返回一个空列表。

有人知道我如何获得这些次要图像的 src 吗?

提前致谢!

【问题讨论】:

    标签: scrapy scrapy-spider splash-screen


    【解决方案1】:

    如果您使用 Splash 来呈现页面(如您所述),您的 xpath 应该可以正常工作。 你可以像这样在scrapy shell中测试它:

    scrapy shell 'http://localhost:8050/render.html?url=https://www2.hm.com/nl_nl/productpage.0220094001.html&timeout=100&wait=0.5'
    

    您的 xpath 提供了 5 个图像的列表:

    response.xpath('//figure[contains(@class, "secondary")]//img//@src').extract()
    ['//lp2.hm.com/hmgoepprod?set=source[/04/2f/042fded3bfa88dda5680a6470f12f180bca4739e.jpg],origin[dam],category[ladies_dresses_maxidresses],type[LOOKBOOK],res[m],res[s],hmver[1]&call=url[file:/product/main]', 
    '//lp2.hm.com/hmgoepprod?set=source[/8b/03/8b03a5491cc1fdfd27c7a308ad8fab2e1d408754.jpg],origin[dam],category[ladies_dresses_maxidresses],type[LOOKBOOK],res[m],res[s],hmver[1]&call=url[file:/product/main]', 
    '//lp2.hm.com/hmgoepprod?set=source[/11/28/1128425ddada72796f5f4ad3b46d5836957f9522.jpg],origin[dam],category[ladies_dresses_maxidresses],type[LOOKBOOK],res[m],res[s],hmver[1]&call=url[file:/product/main]', 
    '//lp2.hm.com/hmgoepprod?set=source[/79/63/796367ae5cfea46bf6b8eb2e1afe2fb5e54e86f4.jpg],origin[dam],category[ladies_dresses_maxidresses],type[DESCRIPTIVESTILLLIFE],res[s],hmver[1]&call=url[file:/product/main]', 
    '//lp2.hm.com/hmprod?set=source[/model/2017/E00 0220094 001 08 3075.jpg],width[843],height[985],x[353],y[123],type[DETAIL],res[s]&hmver=0&call=url[file:/product/main]']
    

    【讨论】:

    • 出于某种原因,这对我不起作用。我得到一个空列表:在 [14]: fetch('192.168.99.101:8050/render.html?url=https://www2.hm.com/nl_nl/… ...: t=100&wait=0.5') 2019-02-06 19:43:45 [scrapy.core.engine] DEBUG: Crawled (400 ) 192.168.99.101:8050/render.html?url=https://www2.hm.com/nl_nl/…> (referer: None) In [15]: response.xpath('//figure[contains(@class, "secondary")]//img//@src').extract() Out [15]:[]
    • 如果你使用标准设置运行scrapy splash,我认为你只能使用90的最大超时。你可以这样试试吗? scrapy shell 'http://localhost:8050/render.html?url=https://www2.hm.com/nl_nl/productpage.0220094001.html&timeout=90&wait=0.5'
    【解决方案2】:

    检查response.xpath('//script[contains(text(), "productArticleDetails =")]/text()') 中的json。它包含有关页面上产品的所有数据,并带有图像。

    【讨论】:

    • 如何从 json 中提取所需的链接?因为返回的文本中没有类或 id。
    • json.loads(response.text)。有关详细信息,请参阅 Python 的 json 模块的文档。
    • 如果使用这种方法,Splash应该是没有必要的。
    猜你喜欢
    • 2020-11-14
    • 1970-01-01
    • 2017-05-27
    • 1970-01-01
    • 1970-01-01
    • 2021-12-20
    • 1970-01-01
    • 2021-11-24
    • 1970-01-01
    相关资源
    最近更新 更多