scrapy回调函数传递参数

scrapy.Request 的callback传参的两种方式

1.使用 lambda方式传递参数

def parse(self, response):
    for sel in response.xpath('//li[@class="clearfix"]/div[@class="list_con"]'):
        item=DmozItem()
        item['href']=sel.xpath('h2/a/@href').extract()[0]
        yield scrapy.Request(item['href'], callback=lambda response, it=item: self.others_parse(response,it),dont_filter=True)
        yield item


def others_parse(self, response, it):
    it['url'] = response.url
    yield it

2.在某些情况下,您可能有兴趣向这些回调函数传递参数,以便稍后在第二个回调中接收参数。您可以使用该Request.meta属性。

def parse(self, response):
    for sel in response.xpath('//li[@class="clearfix"]/div[@class="list_con"]'):
        item=DmozItem()
        item['href']=sel.xpath('h2/a/@href').extract()[0]

        request= scrapy.Request(item['href'], callback=others_parse,dont_filter=True)
        request.meta['item'] = item
        yield request


def others_parse(self, response):
    item = response.meta['item']
    item['other_url'] = response.url
    yield item

https://www.jianshu.com/p/461d74641e80

 

转载于:https://www.cnblogs.com/rgcLOVEyaya/p/RGC_LOVE_YAYA_540days.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值