【问题标题】:Can't scrape an image url from Zara无法从 Zara 抓取图片网址
【发布时间】:2022-01-27 08:29:00
【问题描述】:

我正在尝试从 Zara 抓取图像 url,但我得到的唯一想法是透明背景的 url。

这是我试图抓取的链接:https://static.zara.net/photos///2022/V/0/1/p/9598/176/406/2/w/850/9598176406_1_1_1.jpg?ts=1640187784252 这是我不断得到的链接: https://static.zara.net/stdstatic/1.249.0-b.13/images/transparent-background.png'

有什么想法吗?这是我的代码。先感谢您!! *注意:我在图片中使用了extract(),而不是extract_first(),看是否有几个链接,但都是一样的。

导入scrapy 从 scrapy.linkextractors 导入 LinkExtractor

from Zara.items import Producto

class ZaraSpider(scrapy.Spider):
    name = 'zara'
    allowed_domains = ['zara.com']
    start_urls = [
    'https://www.zara.com/es/es/jersey-punto-cuello-subido-p09598176.html'
    ]
def parse(self, response):
    
    producto = Producto()
    
    # Extraemos los enlaces
    links = LinkExtractor(
        allow_domains=['zara.com'],
        restrict_xpaths=["//a"],
        allow="/es/es/"
        ).extract_links(response)
    
    outlinks = [] # Lista con todos los enlaces
    for link in links:
        url = link.url
        outlinks.append(url) # Añadimos el enlace en la lista
        yield scrapy.Request(url, callback=self.parse) # Generamos la petición  

    
    product = response.xpath('//meta[@content="product"]').extract()
    if product:
    # Extraemos la url, el nombre del producto, la descripcion y su precio
        producto['url'] = response.request.url
        producto['nombre'] = response.xpath('//h1[@class="product-detail-info__name"]/text()').extract_first()
        producto['precio'] = response.xpath('//span[@class="price__amount-current"]/text()').extract_first()
        producto['descripcion'] = response.xpath('//div[@class="expandable-text__inner-content"]//text()').extract_first()
        
        producto['imagen'] = response.xpath('//img[@class="media-image__image media__wrapper--media"]/@src').extract()
        #producto['links'] = outlinks
    
    yield producto'''

【问题讨论】:

  • start_urls 列表是什么?
  • 我编辑了代码,这是完整的代码。谢谢!

标签: web-scraping xpath scrapy imageurl


【解决方案1】:

所以它是用 javascript 生成的问题。尝试用scrapy shell请求一个网页并查看响应,然后你会发现你可以通过另一种方式找到请求的图片url。

import scrapy
from scrapy.linkextractors import LinkExtractor
# from Zara.items import Producto


class Producto(scrapy.Item):
    url = scrapy.Field()
    nombre = scrapy.Field()
    precio = scrapy.Field()
    descripcion = scrapy.Field()
    imagen = scrapy.Field()
    links = scrapy.Field()


class ZaraSpider(scrapy.Spider):
    name = 'zara'
    allowed_domains = ['zara.com']
    start_urls = [
        'https://www.zara.com/es/es/jersey-punto-cuello-subido-p09598176.html'
    ]

    def parse(self, response):
        producto = Producto()
    
        # Extraemos los enlaces
        links = LinkExtractor(
            allow_domains=['zara.com'],
            restrict_xpaths=["//a"],
            allow="/es/es/"
        ).extract_links(response)
    
        outlinks = []   # Lista con todos los enlaces
        for link in links:
            url = link.url
            outlinks.append(url)    # Añadimos el enlace en la lista
            yield scrapy.Request(url, callback=self.parse)  # Generamos la petición  

        product = response.xpath('//meta[@content="product"]').get()
        if product:
            # Extraemos la url, el nombre del producto, la descripcion y su precio
            producto['url'] = response.request.url
            producto['nombre'] = response.xpath('//h1[@class="product-detail-info__name"]/text()').get()
            producto['precio'] = response.xpath('//span[@class="price__amount-current"]/text()').get()
            producto['descripcion'] = response.xpath('//div[@class="expandable-text__inner-content"]//text()').get()
            producto['imagen'] = response.xpath('//meta[@property="og:image"]/@content').get()
            #producto['links'] = outlinks
    
            yield producto

顺便说一句,请查看CrawlSpider。

【讨论】:

    猜你喜欢
    • 2014-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多