2015-08-27 8 views
7
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 
+0

कोशिश जैसे सज्जाकार पर एक नज़र, है। http://thecodeship.com/patterns/guide-to-python-function-decorators/ – Sumido

+0

तो यूआरएल फ़ील्ड लौटाता है जो 'आइटम' में मौजूद नहीं हैं और आप इन फ़ील्ड को' item' में जोड़ना चाहते हैं और इसे वापस करना चाहते हैं? –

+0

क्या आपने यह काम करने के लिए प्रबंधन किया था? – briankip

उत्तर

14

यह वही आप के लिए 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 की सुविधा शॉर्टकट भी है।

0

मैं Tkinter के अतिरिक्त तर्क पारित होने के साथ एक समान मुद्दा था, और (यहाँ: http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/extra-args.html) काम करने के लिए इस समाधान मिल गया है, आपकी समस्या के लिए परिवर्तित:

def parse(self, response): 
    item = HeroItem() 
    [...] 
    def handler(self = self, response = response, item = item): 
     """ passing as default argument values """ 
     return self.parse_profile(response, item) 
    yield Request(url, callback=handler) 
+1

यह एक खतरनाक सुझाव है। वह 'response.xpath ('// tbody/tr') में पाए गए सभी" आइटम "के माध्यम से लूपिंग कर रहा है। चूंकि अनुरोध कॉलबैक (कभी भी) में पैरामीटर के रूप में कोई आइटम प्रदान नहीं करेगा, इसलिए हैंडलर विधि हमेशा आइटम को डिफ़ॉल्ट के रूप में उपयोग करेगी। दुर्भाग्यवश, कॉलबैक कॉल किए जाने पर आइटम जो कुछ भी होगा * वह अनुरोध नहीं होने पर उस समय क्या था। आपका एकत्रित डेटा अविश्वसनीय और असंगत होगा। – Rejected

+0

@ अस्वीकृत नहीं, फ़ंक्शन हेडर (स्वयं = स्वयं ...) में चर निर्दिष्ट करके यह 'हैंडलर' फ़ंक्शन परिभाषा निष्पादित होने पर चर के मान रखता है। इतने लंबे समय तक 'हैंडलर' की परिभाषा लूप के अंदर है, 'parse_profile' को प्रत्येक आइटम के मूल्यों को फिर से चालू किया जाएगा। –

+0

यह एक अच्छी तरह से सुरुचिपूर्ण समाधान है। –

संबंधित मुद्दे