阅读(4611) (0)

scrapy 2.3 常用统计信息收集器使用

2021-06-10 17:12:29 更新

通过访问Stats Collector ​stats​ 属性。以下是访问统计信息的扩展示例:

class ExtensionThatAccessStats:

    def __init__(self, stats):
        self.stats = stats

    @classmethod
    def from_crawler(cls, crawler):
        return cls(crawler.stats)

设置统计值:

stats.set_value('hostname', socket.gethostname())

增量统计值:

stats.inc_value('custom_count')

仅当大于上一个值时设置stat值::

stats.max_value('max_items_scraped', value)

仅当低于上一个时设置stat值::

stats.min_value('min_free_memory_percent', value)

获取统计值:

>>> stats.get_value('custom_count')
1

获取所有统计信息:

>>> stats.get_stats()
{'custom_count': 1, 'start_time': datetime.datetime(2009, 7, 14, 21, 47, 28, 977139)}