2014-04-09 12 views

risposta

7

Controlla il stats page from the scrapy documentation. La documentazione afferma che Stats Collector, ma potrebbe essere necessario aggiungere al codice spider per poter fare cose con esso.

MODIFICA: A rischio di far saltare la mia tromba, se tu fossi dopo un esempio concreto, ho postato un answer about how to collect failed urls.

EDIT2: dopo molte ricerche su google, apparentemente non sono necessarie importazioni. Basta usare self.crawler.stats.set_value()!

+0

hmm. restituisce "ImportError: impossibile importare il crawler dei nomi". '' File '/usr/local/Cellar/python/2.7.6/Framework/Python.framework/Versions/2.7/lib/python2.7/site-packages/scrapy/stats.py ", riga 1, in da scrapy.project import crawler'' – mattes

+0

Questo è strano. Suppongo che il tuo spider di base funzioni senza errori? – Talvalin

+0

sì. funziona fintanto che non faccio nulla con '' stats''. Ecco un esempio di come appare il mio spider: https://gist.github.com/mattes/10367042 – mattes

1

Con scrapy 0.24 - stats lo uso dal modo seguente:

class TopSearchesSpider(CrawlSpider): 
    name = "topSearches" 
    allowed_domains = ["...domain..."] 

    start_urls = (
     'http://...domain...', 
    ) 

    def __init__(self, stats): 
     super(TopSearchesSpider, self).__init__() 
     self.stats = stats 

    @classmethod 
    def from_crawler(cls, crawler): 
     return cls(crawler.stats) 

    def parse_start_url(self, response): 
     sel = Selector(response); 
     url = response.url; 

     self.stats.inc_value('pages_crawled') 
     ... 

Super metodo è quello di chiamare il costruttore CrawlSpider per eseguire il proprio codice.

0

inserire all'interno del codice categoria ragno

def my_parse(self, response): 
    print self.crawler.stats.get_stats() 
Problemi correlati