UPD:不是很接近的问题,因为我认为我的方式还不够清晰
是否可以获得当前请求 + 响应 + 下载时间以将其保存到项目?
在“普通”python中我做
start_time = time()
urllib2.urlopen('http://example.com').read()
time() - start_time
但是我怎么能用 Scrapy 做到这一点?
更新:
解决方案对我来说足够了,但我不确定结果的质量。如果你有很多超时错误的连接Download time
可能是错误的(甚至是DOWNLOAD_TIMEOUT * 3)
为了
设置.py
DOWNLOADER_MIDDLEWARES = {
'myscraper.middlewares.DownloadTimer': 0,
}
中间件.py
from time import time
from scrapy.http import Response
class DownloadTimer(object):
def process_request(self, request, spider):
request.meta['__start_time'] = time()
# this not block middlewares which are has greater number then this
return None
def process_response(self, request, response, spider):
request.meta['__end_time'] = time()
return response # return response coz we should
def process_exception(self, request, exception, spider):
request.meta['__end_time'] = time()
return Response(
url=request.url,
status=110,
request=request)
在spider.py里面def parse(...
log.msg('Download time: %.2f - %.2f = %.2f' % (
response.meta['__end_time'], response.meta['__start_time'],
response.meta['__end_time'] - response.meta['__start_time']
), level=log.DEBUG)