Scrapy 修改爬虫结束时统计数据中的时间为当前系统时间

Scrapy 修改爬虫结束时统计数据中的时间为当前系统时间

一、问题背景

scrapy 在每次运行结束后都会显示一堆统计数据信息,其中是有统计时间数据的,但是!!!那个时间是 UTC 时间(0时区),不是我们平时习惯的系统本地时间,而且里面的爬虫总运行时间是以秒计算的,不符合我们的日常习惯,于是我翻了下scrapy 的源码,找到其中相关的内容并将其重写了一遍,感觉还行,各位看官随缘取之!

二、问题分析

通过日志信息,找到对应的统计爬虫运行时间的类:scrapy.extensions.corestats.CoreStats

  • 日志信息显示如下:
    # 扩展配置
    2021-05-10 10:43:50 [scrapy.middleware] INFO: Enabled extensions:
    ['scrapy.extensions.corestats.CoreStats',  # 信号收集器,里面有记录爬虫的运行时间信息
     'scrapy.extensions.telnet.TelnetConsole',
     'scrapy.extensions.logstats.LogStats']
    # 统计信息
    2021-05-10 10:44:10 [scrapy.statscollectors] INFO: Dumping Scrapy stats:
    {'downloader/exception_count': 3,
     'downloader/exception_type_count/twisted.internet.error.ConnectionRefusedError': 2,
     'downloader/exception_type_count/twisted.internet.error.TimeoutError': 1,
     'downloader/request_bytes': 1348,
     'downloader/request_count': 4,
     'downloader/request_method_count/GET': 4,
     'downloader/response_bytes': 10256,
     'downloader/response_count': 1,
     'downloader/response_status_count/200': 1,
     'elapsed_time_seconds': 18.806005,  # 爬虫运行总耗时
     'finish_reason': 'finished',
     'finish_time': datetime.datetime(2021, 5, 10, 2, 44, 10, 418573),  # 爬虫结束时间
     'httpcompression/response_bytes': 51138,
     'httpcompression/response_count': 1,
     'log_count/INFO': 10,
     'response_received_count': 1,
     'scheduler/dequeued': 4,
     'scheduler/dequeued/memory': 4,
     'scheduler/enqueued': 4,
     'scheduler/enqueued/memory': 4,
     'start_time': datetime.datetime(2021, 5, 10, 2, 43, 51, 612568)}  # 爬虫开始时间
    2021-05-10 10:44:10 [scrapy.core.engine] INFO: Spider closed (finished)
    
  • 源码截图如下:
    信号收集器

三、解决方法

  1. 重写 CoreStats

    # -*- coding: utf-8 -*-
    # 重写信号收集器
    import time
    from scrapy.extensions.corestats import CoreStats
    
    
    class MyCoreStats(CoreStats):
    
        def spider_opened(self, spider):
            """爬虫开始运行"""
            self.start_time = time.time()
            start_time_str = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(self.start_time))  # 转化格式
            self.stats.set_value('爬虫开始时间: ', start_time_str, spider=spider)
    
        def spider_closed(self, spider, reason):
            """爬虫结束运行"""
            # 爬虫结束时间
            finish_time = time.time()
            # 转化时间格式
            finish_time_str = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(finish_time))
            # 计算爬虫运行总耗时
            elapsed_time = finish_time - self.start_time
            m, s = divmod(elapsed_time, 60)
            h, m = divmod(m, 60)
            self.stats.set_value('爬虫结束时间: ', finish_time_str, spider=spider)
            self.stats.set_value('爬虫运行总耗时: ', '%d时:%02d分:%02d秒' % (h, m, s), spider=spider)
            self.stats.set_value('爬虫结束原因: ', reason, spider=spider)
    
  2. 修改配置文件信息

    EXTENSIONS = {
       'scrapy.extensions.corestats.CoreStats': None,  # 禁用默认的数据收集器
       '项目名.extensions.corestats.MyCoreStats': 500,  # 启用自定义的信号收集器
    }
    

四、效果展示

	2021-05-10 11:11:03 [scrapy.statscollectors] INFO: Dumping Scrapy stats:
	{'downloader/exception_count': 5,
	 'downloader/exception_type_count/twisted.internet.error.ConnectionRefusedError': 3,
	 'downloader/exception_type_count/twisted.internet.error.TimeoutError': 2,
	 'downloader/request_bytes': 1976,
	 'downloader/request_count': 6,
	 'downloader/request_method_count/GET': 6,
	 'downloader/response_bytes': 10266,
	 'downloader/response_count': 1,
	 'downloader/response_status_count/200': 1,
	 'httpcompression/response_bytes': 51139,
	 'httpcompression/response_count': 1,
	 'log_count/INFO': 10,
	 'response_received_count': 1,
	 'scheduler/dequeued': 6,
	 'scheduler/dequeued/memory': 6,
	 'scheduler/enqueued': 6,
	 'scheduler/enqueued/memory': 6,
	 '爬虫结束原因': 'finished',
	 '爬虫开始时间: ': '2021-05-10 11:10:39',
	 '爬虫结束时间: ': '2021-05-10 11:11:03',
	 '爬虫运行总耗时: ': '0时:00分:24秒'}
  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值