2016-09-21 3 views
0

치료 파이프 라인에 문제가 있습니다. EnricherPipeline이 절대로 시작되지 않습니다. 나는 process_item의 첫 번째 줄에 디버거를 넣었고 제어권을 얻지 못했다. JsonPipeline 시작 않지만, 수신하는 첫 번째 인수는 유형 generator object process_item이다 아닌 MatchItem 인스턴스가 내가 EnricherPipeline을 사용하지 않는 경우 예상대로 JsonPipeline 작동 (수신한다.치료 파이프 라인이 시작되지 않음

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의 그것은 결코 enteres. 그것은 딕트 (dict)를 반환하기로되어 있습니다. – FranGoitia

+0

_ 놀랍게도, 치료사는 결코 그 파이프 라인에 들어 가지 않습니다. _ 게시 한 코드가 단일 파일 (예 :'pipelines.py')이거나 다른 파일입니까? 'ITEM_PIPELINES'는'settings.py' 파일에서 설정 될 것으로 예상됩니다. – starrify

답변

0

좋아, 그래서 문제는 것이 었습니다 EnricherPipeline 디버그가 첫 번째 파이프 라인에서 작동하지 않는 이유를 아직도 이해할 수는 없지만 예상대로 작동합니다.

+1

스파이더가 결과를 산출하지 못하면 파이프 라인의'process_item()'이 호출되지 않으므로 디버거의 중단 점에 도달하지 못합니다. – Granitosaurus

+0

거미가 결과를 산출합니다. – FranGoitia

+0

첫 번째 파이프 라인이 호출되는지 여부를 알려주는 몇 가지 방법이 있습니다. (에이). 치료 로그를 통해 파이프 라인이 활성화되었는지 여부를 확인합니다. (b) process_item() 함수에서 인쇄 또는 로깅을 한 다음이 함수가 호출되는지 확인하십시오. – rojeeer

관련 문제