Search code examples
pythondictionaryscrapykeyerrorscrapy-pipeline

Can't get value from Scrapy stats dictionary


I have this pipeline in my scrapy where I need to get an info from the Scrapy stats

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

When I run the code, I get this error

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'

If that's not the right way to get the stats value, then what should I do?


Solution

  • Found the answer! Finally!

    Instead of self.stats.get_stats()['item_scraped_count'] use self.stats.get_value('item_scraped_count')

    https://doc.scrapy.org/en/latest/topics/stats.html