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
Run Code Online (Sandbox Code Playgroud)
好吧,我正在主解析方法中抓取整个表格,我从该表中取了几个字段.其中一个字段是一个网址,我想探索它以获得一大堆字段.如何将已创建的ITEM对象传递给回调函数,以便最终项保留所有字段?
正如上面的代码中所示,我能够保存url中的字段(目前的代码)或只保存表中的字段(只是写yield item)但我不能只生成一个包含所有字段的对象一起.
我试过这个,但很明显,它不起作用.
yield Request(url, callback=self.parse_profile(item))
def parse_profile(self, response, item): …Run Code Online (Sandbox Code Playgroud)