Recording the total time taken for running a spider in scrapy

前端 未结 3 1976
Happy的楠姐
Happy的楠姐 2021-01-12 17:18

I am using scrapy to scrap a site

I had written a spider and fetched all the items from the page and saved to a csv file, and now i want to save the total exec

3条回答
  •  时光取名叫无心
    2021-01-12 17:53

    The easiest way I've found so far:

    import scrapy
    
    class StackoverflowSpider(scrapy.Spider):
        name = "stackoverflow"
    
        start_urls = ['https://stackoverflow.com/questions/tagged/web-scraping']
    
        def parse(self, response):
            for title in response.css(".summary .question-hyperlink::text").getall():
                yield {"Title":title}
    
        def close(self, reason):
            start_time = self.crawler.stats.get_value('start_time')
            finish_time = self.crawler.stats.get_value('finish_time')
            print("Total run time: ", finish_time-start_time)
    

提交回复
热议问题