python - 将参数传递给回调函数

标签 python callback arguments scrapy

def parse(self, response):
    for sel in response.xpath('//tbody/tr'):
        item = HeroItem()
        item['hclass'] = response.request.url.split("/")[8].split('-')[-1]
        item['server'] = response.request.url.split('/')[2].split('.')[0]
        item['hardcore'] = len(response.request.url.split("/")[8].split('-')) == 3
        item['seasonal'] = response.request.url.split("/")[6] == 'season'
        item['rank'] = sel.xpath('td[@class="cell-Rank"]/text()').extract()[0].strip()
        item['battle_tag'] = sel.xpath('td[@class="cell-BattleTag"]//a/text()').extract()[1].strip()
        item['grift'] = sel.xpath('td[@class="cell-RiftLevel"]/text()').extract()[0].strip()
        item['time'] = sel.xpath('td[@class="cell-RiftTime"]/text()').extract()[0].strip()
        item['date'] = sel.xpath('td[@class="cell-RiftTime"]/text()').extract()[0].strip()
        url = 'https://' + item['server'] + '.battle.net/' + sel.xpath('td[@class="cell-BattleTag"]//a/@href').extract()[0].strip()

        yield Request(url, callback=self.parse_profile)

def parse_profile(self, response):
    sel = Selector(response)
    item = HeroItem()
    item['weapon'] = sel.xpath('//li[@class="slot-mainHand"]/a[@class="slot-link"]/@href').extract()[0].split('/')[4]
    return item

好吧,我在主要的解析方法中抓取了整个表,并且从该表中提取了几个字段。其中一个字段是一个 url,我想探索它以获得一大堆新的字段。如何将已创建的 ITEM 对象传递给回调函数,以便最终项目保留所有字段?

如上面的代码所示,我可以保存 url 中的字段(目前是代码)或仅保存表中的字段(只需编写 yield item) 但我不能只生成一个包含所有字段的对象。

我已经试过了,但显然,它不起作用。

yield Request(url, callback=self.parse_profile(item))

def parse_profile(self, response, item):
    sel = Selector(response)
    item['weapon'] = sel.xpath('//li[@class="slot-mainHand"]/a[@class="slot-link"]/@href').extract()[0].split('/')[4]
    return item

最佳答案

这就是您要使用 meta 关键字的目的。

def parse(self, response):
    for sel in response.xpath('//tbody/tr'):
        item = HeroItem()
        # Item assignment here
        url = 'https://' + item['server'] + '.battle.net/' + sel.xpath('td[@class="cell-BattleTag"]//a/@href').extract()[0].strip()

        yield Request(url, callback=self.parse_profile, meta={'hero_item': item})

def parse_profile(self, response):
    item = response.meta.get('hero_item')
    item['weapon'] = response.xpath('//li[@class="slot-mainHand"]/a[@class="slot-link"]/@href').extract()[0].split('/')[4]
    yield item

另请注意,执行 sel = Selector(response) 是一种资源浪费,并且与您之前所做的不同,因此我对其进行了更改。它在 response 中自动映射为 response.selector,它还有 response.xpath 的便捷快捷方式。

关于python - 将参数传递给回调函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32252201/

相关文章:

python - Pandas,仅合并同一组中的行

mysql - 在 ruby​​ 中连接到 mysql 会产生错误数量的参数错误(4 个,共 0 个)- 如何调试?

javascript - 传递一个函数作为参数,它使用父函数的参数,但也有它自己的参数

Python 单词和短语的共现矩阵

python - 忽略指定值的 numpy 数组的平均值

javascript - Node JS 匿名函数和回调

android - setOnQueryTextFocusChangeListener 与 setOnFocusChangeListener

将可变数量的参数插入到字符串中的 Pythonic 操作

python - 使用python从列表中随机提取x个项目

JavaScript 闭包和作用域 : How to pass an outer scope variable to a callback function which is passed as an argument?