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
嗯,我在主分析方法中抓取整个表格,并且从该表格取得了几个字段。其中一个领域是一个网址,我想探索它来获得一大堆领域。我如何将我已经创建的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
尝试看看装饰,如。 http://thecodeship.com/patterns/guide-to-python-function-decorators/ – Sumido
因此,url会返回'item'中不存在的字段,并且您希望将这些字段添加到'item'并返回它? –
你是否设法使这项工作? – briankip