博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
scrapy回调函数传递参数
阅读量:7093 次
发布时间:2019-06-28

本文共 1034 字,大约阅读时间需要 3 分钟。

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 itemdef 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 requestdef others_parse(self, response):    item = response.meta['item']    item['other_url'] = response.url    yield item

 

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

你可能感兴趣的文章
BeautifulSoup
查看>>
详细设计(改)
查看>>
5步减小你的CSS样式表
查看>>
实现简单的PHP接口,以及使用js/jquery ajax技术调用此接口
查看>>
PL/SQL Developer 远程连接Oracle数据库
查看>>
正则表达式汇总
查看>>
ES6中对对象的扩展
查看>>
Java遇见HTML——JSP篇之JSP基础语法
查看>>
Java中Native关键字的作用
查看>>
感觉挺有意思的SQL题目
查看>>
真tm无聊,这几天。。。
查看>>
ansible become与sudo
查看>>
PIE SDK地图图层控制
查看>>
UVa 1339 - Ancient Cipher
查看>>
微服务架构 SpringCloud(四)Ribbon
查看>>
PAT天梯赛L3-007 天梯地图
查看>>
两年之中
查看>>
【翻译】使用Visual Studio在Azure上部署Asp.Net Core Web应用
查看>>
Switch Game
查看>>
整数A和B的二进制表示有多少位不同
查看>>