2016-09-21 141 views
0

我在使用Scrapy管道時遇到問題。 EnricherPipeline從未開始。我在process_item的第一行放了一個調試器,它從來沒有得到控制權。 JsonPipeline不啓動,但它接收到的第一個參數是generator object process_item型的,而不是MatchItem比如,它應該接受(當我禁用EnricherPipeline,JsonPipeline按預期工作。Scrapy管道未啓動

class MatchSpider(CrawlSpider): 

    def parse(self, response): 
     browser = Browser(browser='Chrome') 
     browser.get(response.url) 
     browser.find_element_by_xpath('//a[contains(text(), "{l}") and @title="{c}"]'.format(l=self.league, c=self.country)).click() 
     browser.find_element_by_xpath('//select[@id="seasons"]/option[text()="{s}"]'.format(s=self.season.replace('-', '/'))).click() 
     browser.find_element_by_xpath('//a[contains(text(), "Fixture")]').click() 
     page_matches = browser.find_elements_by_xpath('//*[contains(@class, "result-1 rc")]') 
     matches.extend([m.get_attribute('href') for m in page_matches] 
     for m in matches[:1]: 
      yield Request(m, callback=self.process_match, dont_filter=True) 

    def process_match(self, response): 
     match_item = MatchItem() 
     match_item['url'] = response.url 
     match_item['project'] = self.settings.get('BOT_NAME') 
     match_item['spider'] = self.name 
     match_item['server'] = socket.gethostname() 
     match_item['date'] = datetime.datetime.now() 
     return match_item 

class EnricherPipeline: 
    def process_item(self, item, spider): 
     self.match = defaultdict(dict) 
     self.match['date'] = item['match']['startTime'] 
     self.match['referee'] = item['match']['refereeName'] 
     self.match['stadium'] = item['match']['venueName'] 
     self.match['exp_mins'] = item['match']['expandedMinutes'] 
     yield self.match 


class JsonPipeline: 

    def process_item(self, item, scraper): 
     output_dir = 'data/matches/{league}/{season}'.format(league=scraper.league, season=scraper.season) 
     if not os.path.exists(output_dir): 
      os.makedirs(output_dir) 
     file_name = "-".join([str(datetime.strptime(item['date'], '%Y-%m-%dT%H:%M:%S').date()), 
           item['home']['name'], item['away']['name']]) + '.json' 
     item_path = os.sep.join((output_dir, file_name)) 
     with open(item_path, 'w') as f: 
      f.write(json.dumps(item)) 



ITEM_PIPELINES = { 
    'scrapers.whoscored.whoscored.pipelines.EnricherPipeline': 300, 
    'scrapers.whoscored.whoscored.pipelines.JsonPipeline': 800, 
} 
+0

您有另一個管道'scrapers.whoscored.whoscored.pipelines。 EnricherPipeline':300.這條管道返回什麼? – rojeeer

+0

奇怪的是,Scrapy從來沒有進入這個管道,我在第一行放了一個調試器process_item,它永遠不會進入。它應該返回一個字典。 – FranGoitia

+0

_奇怪的是,Scrapy從來沒有進入這個管道._你是從一個單獨的文件(例如'pipelines.py')發佈的代碼還是不同的文件? 'ITEM_PIPELINES'預計將在'settings.py'文件中設置。 – starrify

回答

0

好了,問題是,EnricherPipeline但是我仍然不明白爲什麼調試器不在第一個管道中工作

+1

如果你的蜘蛛從來沒有得到任何結果,那麼管道的'process_item()'永遠不會被調用,因此調試器的斷點永遠不會到達。 – Granitosaurus

+0

我的蜘蛛確實產生了結果。 – FranGoitia

+0

有幾種方法可以判斷第一個管道是否被調用。 (一個)。通過scrapy日誌,檢查您的管道是否啓用。 (b)在process_item()函數中,執行一些打印或記錄,然後檢查是否調用此函數。 – rojeeer