Ami*_*ini 0 python dictionary scrapy keyerror scrapy-pipeline
我的 scrapy 中有这个管道,我需要从 Scrapy 统计信息中获取信息
class MyPipeline(object):
def __init__(self, stats):
self.stats = stats
@classmethod
def from_crawler(cls, crawler):
return cls(crawler.stats)
def process_item(self, item, spider):
print self.stats.get_stats()['item_scraped_count']
return item
Run Code Online (Sandbox Code Playgroud)
当我运行代码时,我收到此错误
Traceback (most recent call last):
File "D:\Kerja\HIT\PYTHON~1\<project_name>\<project_name>\lib\site-packages\twisted\internet\defer.py", line 649, in _runCallbacks
current.result = callback(current.result, *args, **kw)
File "D:\Kerja\HIT\Python Projects\<project_name>\<project_name>\<project_name>\<project_name>\pipelines.py", line 35, in process_item
print self.stats.get_stats()['item_scraped_count']
KeyError: 'item_scraped_count'
Run Code Online (Sandbox Code Playgroud)
如果这不是获取统计值的正确方法,那么我该怎么办?
找到答案了!最后!
而不是self.stats.get_stats()['item_scraped_count']使用self.stats.get_value('item_scraped_count')
https://doc.scrapy.org/en/latest/topics/stats.html