繁体   English   中英

Scrapy请求不会回调

[英]Scrapy request does not callback

我正在尝试创建一个蜘蛛,从csv(两个链接,每行一个名称)获取数据,并从每个链接中抓取一个简单元素(价格),为每一行返回一个项目,项目的名称为CSV中的名称,以及两个报废价格(每个链接一个)。

一切都按预期进行,除了事实是不返回价格,而是从每个请求的回调函数返回价格,我得到了一个请求对象,如下所示:

<获取https://link.com > ..

根本不调用回调函数,为什么呢?

这是蜘蛛:

f = open('data.csv')
f_reader = csv.reader(f)
f_data = list(f_reader)

parsed_data = []

for product in f_data:
    product = product[0].split(';')
    parsed_data.append(product)

f.close()

class ProductSpider(scrapy.Spider):
    name = 'products'
    allowed_domains = ['domain1', 'domain2']

    start_urls = ["domain1_but_its_fairly_useless"]

    def parse(self, response):
        global parsed_data
        for product in parsed_data:

            item = Product()

            item['name'] = product[0]
            item['first_price'] = scrapy.Request(product[1], callback=self.parse_first)
            item['second_price'] = scrapy.Request(product[2], callback=self.parse_second)
            yield item


    def parse_first(self, response):
        digits = response.css('.price_info .price span').extract()
        decimals = response.css('.price_info .price .price_demicals').extract()
        yield float(str(digits)+'.'+str(decimals))

    def parse_second(self, response):
        digits = response.css('.lr-prod-pricebox-price .lr-prod-pricebox-price-primary span[itemprop="price"]').extract()
        yield digits

在此先感谢您的帮助!

TL; DR:您应该在产生项目或请求时产生其中带有Request对象的项目。


长版:
蜘蛛中的解析方法应该返回scrapy.Item在这种情况下,该爬网的链将停止并且scrapy将放置一个项目或scrapy.Requests在这种情况下,scrady将安排一个请求以继续链。

Scrapy是异步的,因此从多个请求中创建一个项目意味着您需要将所有这些请求链接在一起,同时将项目携带到每个项目中并一点一点地填满。 Request对象具有meta属性,您可以在其中存储想要存储的任何内容(几乎可以存储),并将其携带到回调函数中。 通常,使用它来链接需要多个请求以形成单个项目的项目的请求。

您的蜘蛛应如下所示:

class ProductSpider(scrapy.Spider):
    # <...>
    def parse(self, response):
        for product in parsed_data:
            item = Product()
            item['name'] = product[0]
            # carry next url you want to crawl in meta
            # and carry your item in meta
            yield Request(product[1], self.parse_first,
                          meta={"product3": product[2], "item":item})  


    def parse_first(self, response):
        # retrieve your item that you made in parse() func
        item = response.meta['item']
        # fill it up
        digits = response.css('.price_info .price span').extract()
        decimals = response.css('.price_info .price .price_demicals').extract()
        item['first_price'] = float(str(digits)+'.'+str(decimals))
        # retrieve next url from meta
        # carry over your item to the next url
        yield Request(response.meta['product3'], self.parse_second,
                      meta={"item":item})


    def parse_second(self, response):
        # again, retrieve your item
        item = response.meta['item']
        # fill it up
        digits = response.css('.lr-prod-pricebox-price .lr-prod-pricebox-price-primary 
                              span[itemprop="price"]').extract()
        item['secodn_price'] = digits
        # and finally return the item after 3 requests! 
        yield item

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM