当前位置:网站首页>Scrapy 修改爬虫结束时统计数据中的时间为当前系统时间
Scrapy 修改爬虫结束时统计数据中的时间为当前系统时间
2022-04-23 06:27:00 【阿兵哥哥】
一、问题背景
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)
- 源码截图如下:
三、解决方法
-
重写
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)
-
修改配置文件信息
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秒'}
版权声明
本文为[阿兵哥哥]所创,转载请带上原文链接,感谢
https://blog.csdn.net/Ayue1220/article/details/116589332
边栏推荐
猜你喜欢
FSM finite state machine
MySQL8.0 安装/卸载 教程【Window10版】
中间人环境mitmproxy搭建
简易随机点名抽奖(js下编写)
移动端布局(3D转换、动画)
Visualization Road (IX) detailed explanation of arrow class
SAP PI / Po rfc2restful Publishing RFC interface as restful examples (proxy indirect)
超级宝典&编程指南(红蓝宝书)-读书笔记
页面实时显示当前时间
设置了body的最大宽度,但是为什么body的背景颜色还铺满整个页面?
随机推荐
3.排序语句
js之什么是事件?事件三要素以及操作元素
Django uses MySQL database to solve error reporting
SVG中年月日相关的表达式
Preliminary configuration of OpenGL super Dictionary (freeglut, glew, gltools, GLUT)
promise all的实现
js之预解析
对js中argumens的简单理解
Reflect on the limitations of event bus and the design and implementation of communication mechanism in component development process
Authorization server (simple construction of authorization server)
2. Restricted query
CSDN很火的汤小洋老师全部课程总共有哪些(问号问号问号)
Apache Hudi 如何加速传统的批处理模式?
instanceof的实现原理
5. Sql99 standard: internal connection and external connection
MySQL isolation level
颜色转换公式大全及转换表格(31种)
异步的学习
C# SmoothProgressBar自定义进度条控件
What are the total number of all courses of Mr. Tang Xiaoyang, who is very popular in CSDN (question mark)