Scrapy-splash 找不到图片源网址

问题描述

我正在尝试从 Zara 抓取产品页面。像这个:https://www.zara.com/us/en/fitted-houndstooth-blazer-p07808160.html?v1=108967877&v2=1718115

我的scrapy-splash 容器正在运行。在shell中我获取页面

fetch('http://localhost:8050/render.html?url=https://www.zara.com/us/en/fitted-houndstooth-blazer-p07808160.html?v1=108967877&v2=1718115')
2021-05-14 14:30:42 [scrapy.core.engine] INFO: Spider opened
2021-05-14 14:30:43 [scrapy.core.engine] DEBUG: Crawled (200) <GET http://localhost:8050/render.html?url=https://www.zara.com/us/en/fitted-houndstooth-blazer-p07808160.html?v1=108967877&v2=1718115> (referer: None)

到目前为止一切正常,我能够获得标题和价格。但是,我想获取产品的图片网址。

我尝试通过

response.css('img.media-image__image::attr(src)').getall()

但回应是这样的:

['https://static.zara.net/stdstatic/1.211.0-b.44/images/transparent-background.png','https://static.zara.net/stdstatic/1.211.0-b.44/images/transparent-background.png','https://static.zara.net/stdstatic/1.211.0-b.44/images/transparent-background.png']

这都是背景图片,而不是真正的图片。我可以在浏览器上显示图像,我看到网络请求中的图像。是因为它加载了 AJAX 请求吗?我该如何解决这个问题?

解决方法

我上周才开始研究网络抓取,所以我不确定我是否能帮上很多忙,但我确实找到了一些东西。

源代码在顶部的脚本中显示了这一点:

_mkt_imageDir = /BASE_IMAGES_URL=(.*?);/.test(document.cookie) && RegExp.$1 || 'https://static.zara.net/photos/';

再往下:

"originalUrl":"/us/en/fitted-houndstooth-blazer-p07808160.html?v1=108967877&v2=1718115","imageBaseUrl":"https://static.zara.net/photos/"

然后这里的所有图像似乎都在 javascript 中:

[{"@context":"http://schema.org/","@type":"Product","sku":"108967877-046-1","name":"FITTED HOUNDSTOOTH BLAZER","mpn":"108967877-046-1","brand":"ZARA","description":"","image":["https://static.zara.net/photos///2021/I/0/1/p/7808/160/046/2/w/1920/7808160046_1_1_1.jpg?ts=1620821843383","https://static.zara.net/photos///2021/I/0/1/p/7808/160/046/2/w/1920/7808160046_2_1_1.jpg?ts=1620821851988","https://static.zara.net/photos///2021/I/0/1/p/7808/160/046/2/w/1920/7808160046_2_2_1.jpg?ts=1620821839280","https://static.zara.net/photos///2021/I/0/1/p/7808/160/046/2/w/1920/7808160046_6_1_1.jpg?ts=1620655538200","https://static.zara.net/photos///2021/I/0/1/p/7808/160/046/2/w/1920/7808160046_6_2_1.jpg?ts=1620655535611","https://static.zara.net/photos///2021/I/0/1/p/7808/160/046/2/w/1920/7808160046_6_3_1.jpg?ts=1620656141718","https://static.zara.net/photos///contents/cm/w/1920/sustainability-extrainfo-label-JL78_0.jpg?ts=1602602200357"]

我不知道您将如何刮擦它们,但是当您发现时我会很想知道答案。

问候塞缪尔

,

@samuelhogg 找到了 json 值得称赞,但这里有一个示例蜘蛛,展示了如何从页面中获取所有图像 URL。请注意,您甚至不需要在这里使用 splash,我还没有使用 splash 对其进行测试,但我认为它应该仍然有效。

from scrapy import Spider
import json


class Zara(Spider):
    name = "zara"
    start_urls = [
        "https://www.zara.com/us/en/fitted-houndstooth-blazer-p07808160.html?v1=108967877&v2=1718115"
    ]
  
    def parse(self,response):
        # Find the json identified by @samuelhogg
        data = response.css("script[type='application/ld+json']::text").get()
        # Make a set of all the images in the json
        images = {image for i in json.loads(data) for image in i["image"]}
        # Do what you want with them!
        print(images)
,

看起来 url 位于 json 文件中,我相信您可以从中抓取 url。 json

有一些关于从 json here 中抓取的信息/代码