Skip to main content
 首页 » 编程设计

python之我怎样才能获得scrapy的抓取速度

2025年05月04日18lyj

我正在使用代理池抓取网站。其中很少有非常快的,但很少有代理非常慢,这让我想知道是否有任何方法可以检测中间件中抓取网页的速度(scrapedPages/分钟),以便我可以丢弃较慢的代理。

我可以看到日志级别 INFO 每分钟后在屏幕上打印此速度。

2015-12-04 11:28:50 [scrapy] INFO: Crawled 10 pages (at 10 pages/min), scraped 10 items (at 10 items/min) 

但我无法在中间件中获得这种速度。到目前为止,这是我尝试过的方法。

class getSpeedstats(object): 
    def __init__(self, stats): 
        self.stats = stats 
 
    @classmethod 
    def from_crawler(cls, crawler): 
        return cls(crawler.stats) 
 
     def process_request(self, request, spider): 
        print self.stats.get_stats() 

上面的代码给出了这个输出:

{'log_count/DEBUG': 784, 'scheduler/dequeued': 408, 'log_count/INFO': 10, 'downloader/response_count': 392, 'downloader/response_status_count/200': 392, 'response_received_count': 392, 'scheduler/enqueued/memory': 408, 'downloader/response_bytes': 3209679, 'start_time': datetime.datetime(2015, 12, 4, 3, 48, 41, 31403), 'scheduler/dequeued/memory': 408, 'scheduler/enqueued': 408, 'downloader/request_bytes': 101321, 'downloader/request_method_count/GET': 407, 'downloader/request_count': 407, 'item_scraped_count': 391} 
 
{'log_count/DEBUG': 786, 'scheduler/dequeued': 409, 'log_count/INFO': 11, 'downloader/response_count': 393, 'downloader/response_status_count/200': 393, 'response_received_count': 393, 'scheduler/enqueued/memory': 409, 'downloader/response_bytes': 3217865, 'start_time': datetime.datetime(2015, 12, 4, 3, 48, 41, 31403), 'scheduler/dequeued/memory': 409, 'scheduler/enqueued': 409, 'downloader/request_bytes': 101575, 'downloader/request_method_count/GET': 408, 'downloader/request_count': 408, 'item_scraped_count': 392} 

但我仍然无法破译如何通过它计算蜘蛛的速度。谁能告诉我是否还有其他方法可以做到这一点?

请您参考如下方法:

这是LogStats扩展中的一个计算量,基本上是每分钟current_pages - previous_pages,你可以查看here :

现在这不是 IMO 的实际速率,因为它只考虑了特定的分钟,并且总的平均速度会更好,因为仅使用:

pages = self.stats.get_value('response_received_count') 
print pages/((datetime.now() - self.stats.get_value('start_time')).seconds/60.0) 

这样就可以得到requests/min的平均速度