Sì, questo è possibile.
Ho aggiunto un elenco failed_urls alla mia classe spider e gli ho aggiunto degli URL se lo stato della risposta era 404 (questo dovrà essere esteso per coprire altri stati di errore).
Quindi ho aggiunto un handle che unisce l'elenco in una stringa singola e lo aggiunge alle statistiche quando lo spider viene chiuso.
In base ai commenti, è possibile tenere traccia degli errori di Twisted.
from scrapy.spider import BaseSpider
from scrapy.xlib.pydispatch import dispatcher
from scrapy import signals
class MySpider(BaseSpider):
handle_httpstatus_list = [404]
name = "myspider"
allowed_domains = ["example.com"]
start_urls = [
'http://www.example.com/thisurlexists.html',
'http://www.example.com/thisurldoesnotexist.html',
'http://www.example.com/neitherdoesthisone.html'
]
def __init__(self, category=None):
self.failed_urls = []
def parse(self, response):
if response.status == 404:
self.crawler.stats.inc_value('failed_url_count')
self.failed_urls.append(response.url)
def handle_spider_closed(spider, reason):
self.crawler.stats.set_value('failed_urls', ','.join(spider.failed_urls))
def process_exception(self, response, exception, spider):
ex_class = "%s.%s" % (exception.__class__.__module__, exception.__class__.__name__)
self.crawler.stats.inc_value('downloader/exception_count', spider=spider)
self.crawler.stats.inc_value('downloader/exception_type_count/%s' % ex_class, spider=spider)
dispatcher.connect(handle_spider_closed, signals.spider_closed)
uscita (il downloader/exception_count * statistiche verranno visualizzati solo se le eccezioni sono in realtà gettati - li ho simulato cercando di eseguire il ragno dopo che avevo spento la mia scheda di rete wireless):
2012-12-10 11:15:26+0000 [myspider] INFO: Dumping Scrapy stats:
{'downloader/exception_count': 15,
'downloader/exception_type_count/twisted.internet.error.DNSLookupError': 15,
'downloader/request_bytes': 717,
'downloader/request_count': 3,
'downloader/request_method_count/GET': 3,
'downloader/response_bytes': 15209,
'downloader/response_count': 3,
'downloader/response_status_count/200': 1,
'downloader/response_status_count/404': 2,
'failed_url_count': 2,
'failed_urls': 'http://www.example.com/thisurldoesnotexist.html, http://www.example.com/neitherdoesthisone.html'
'finish_reason': 'finished',
'finish_time': datetime.datetime(2012, 12, 10, 11, 15, 26, 874000),
'log_count/DEBUG': 9,
'log_count/ERROR': 2,
'log_count/INFO': 4,
'response_received_count': 3,
'scheduler/dequeued': 3,
'scheduler/dequeued/memory': 3,
'scheduler/enqueued': 3,
'scheduler/enqueued/memory': 3,
'spider_exceptions/NameError': 2,
'start_time': datetime.datetime(2012, 12, 10, 11, 15, 26, 560000)}
Questo non funziona più. 'exceptions.NameError: il nome globale 'self' non è definito 'si verifica un errore. 'BaseSpider' ora è semplicemente' Spider' http://doc.scrapy.org/en/0.24/news.html?highlight = basespider # id2 https://github.com/scrapy/dirbot/blob/master/dirbot/spiders/dmoz.py ma non riesco a trovare la soluzione per far funzionare il codice ancora @Talvalin. – Mikeumus