Scrapy管道未启动
我在使用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,
}
好了,问题是,EnricherPipeline但是我仍然不明白为什么调试器不在第一个管道中工作
如果你的蜘蛛从来没有得到任何结果,那么管道的'process_item()'永远不会被调用,因此调试器的断点永远不会到达。 – Granitosaurus
我的蜘蛛确实产生了结果。 – FranGoitia
有几种方法可以判断第一个管道是否被调用。 (一个)。通过scrapy日志,检查您的管道是否启用。 (b)在process_item()函数中,执行一些打印或记录,然后检查是否调用此函数。 – rojeeer
您有另一个管道'scrapers.whoscored.whoscored.pipelines。 EnricherPipeline':300.这条管道返回什么? – rojeeer
奇怪的是,Scrapy从来没有进入这个管道,我在第一行放了一个调试器process_item,它永远不会进入。它应该返回一个字典。 – FranGoitia
_奇怪的是,Scrapy从来没有进入这个管道._你是从一个单独的文件(例如'pipelines.py')发布的代码还是不同的文件? 'ITEM_PIPELINES'预计将在'settings.py'文件中设置。 – starrify