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
ठीक है, मैं मुख्य पार्स विधि में एक पूरी तालिका को स्क्रैप कर रहा हूं और मैंने उस तालिका से कई फ़ील्ड ले लिए हैं। इन क्षेत्रों में से एक यूआरएल है और मैं इसे खेतों का एक नया समूह पाने के लिए खोजना चाहता हूं। मैं कॉलबैक फ़ंक्शन में अपनी पहले से बनाई गई आईटीईएम ऑब्जेक्ट को कैसे पास कर सकता हूं ताकि अंतिम आइटम सभी फ़ील्ड रख सके?कॉलबैक फ़ंक्शन
जैसा कि यह उपरोक्त कोड में दिखाया गया है, मैं यूआरएल (पल में कोड) या केवल तालिका में वाले फ़ील्ड को सहेजने में सक्षम हूं (बस 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
तो यूआरएल फ़ील्ड लौटाता है जो 'आइटम' में मौजूद नहीं हैं और आप इन फ़ील्ड को' item' में जोड़ना चाहते हैं और इसे वापस करना चाहते हैं? –
क्या आपने यह काम करने के लिए प्रबंधन किया था? – briankip