PHA PHA - 6 months ago 25
Python Question

How many items has been scraped per start_url

I use scrapy to crawl 1000 urls and store scraped item in a mongodb. I'd to know how many items have been found for each url. From scrapy stats I can see

'item_scraped_count': 3500

However, I need this count for each start_url separately. There is also
referer
field for each item that I might use to count each url items manually:

2016-05-24 15:15:10 [scrapy] DEBUG: Crawled (200) <GET https://www.youtube.com/watch?v=6w-_ucPV674> (referer: https://www.youtube.com/results?q=billys&sp=EgQIAhAB)


But I wonder if there is a built-in support from scrapy.

Answer

challenge accepted!

there isn't something on scrapy that directly supports this, but you could separate it from your spider code with a Spider Middleware:

middlewares.py

from scrapy.http.request import Request

class StartRequestsCountMiddleware(object):

    start_urls = {}

    def process_start_requests(self, start_requests, spider):
        for i, request in enumerate(start_requests):
            self.start_urls[i] = request.url
            request.meta.update(start_request_index=i)
            yield request

    def process_spider_output(self, response, result, spider):
        for output in result:
            if isinstance(output, Request):
                output.meta.update(
                    start_request_index=response.meta['start_request_index'],
                )
            else:
                spider.crawler.stats.inc_value(
                    'start_requests/item_scraped_count/{}'.format(
                        self.start_urls[response.meta['start_request_index']],
                    ),
                )
            yield output

Remember to activate it on settings.py:

SPIDER_MIDDLEWARES = {
    ...
    'myproject.middlewares.StartRequestsCountMiddleware': 200,
}

Now you should be able to see something like this on your spider stats:

'start_requests/item_scraped_count/START_URL1': ITEMCOUNT1,
'start_requests/item_scraped_count/START_URL2': ITEMCOUNT2,