Debugging Spiders

This document explains the most common techniques for debugging spiders. Consider the following Scrapy spider below:

  1. import scrapy
  2. from myproject.items import MyItem
  3. class MySpider(scrapy.Spider):
  4. name = 'myspider'
  5. start_urls = (
  6. 'http://example.com/page1',
  7. 'http://example.com/page2',
  8. )
  9. def parse(self, response):
  10. # <processing code not shown>
  11. # collect `item_urls`
  12. for item_url in item_urls:
  13. yield scrapy.Request(item_url, self.parse_item)
  14. def parse_item(self, response):
  15. # <processing code not shown>
  16. item = MyItem()
  17. # populate `item` fields
  18. # and extract item_details_url
  19. yield scrapy.Request(item_details_url, self.parse_details, cb_kwargs={'item': item})
  20. def parse_details(self, response, item):
  21. # populate more `item` fields
  22. return item

Basically this is a simple spider which parses two pages of items (the start_urls). Items also have a details page with additional information, so we use the cb_kwargs functionality of Request to pass a partially populated item.

Parse Command

The most basic way of checking the output of your spider is to use the parse command. It allows to check the behaviour of different parts of the spider at the method level. It has the advantage of being flexible and simple to use, but does not allow debugging code inside a method.

In order to see the item scraped from a specific url:

  1. $ scrapy parse --spider=myspider -c parse_item -d 2 <item_url>
  2. [ ... scrapy log lines crawling example.com spider ... ]
  3. >>> STATUS DEPTH LEVEL 2 <<<
  4. # Scraped Items ------------------------------------------------------------
  5. [{'url': <item_url>}]
  6. # Requests -----------------------------------------------------------------
  7. []

Using the --verbose or -v option we can see the status at each depth level:

  1. $ scrapy parse --spider=myspider -c parse_item -d 2 -v <item_url>
  2. [ ... scrapy log lines crawling example.com spider ... ]
  3. >>> DEPTH LEVEL: 1 <<<
  4. # Scraped Items ------------------------------------------------------------
  5. []
  6. # Requests -----------------------------------------------------------------
  7. [<GET item_details_url>]
  8. >>> DEPTH LEVEL: 2 <<<
  9. # Scraped Items ------------------------------------------------------------
  10. [{'url': <item_url>}]
  11. # Requests -----------------------------------------------------------------
  12. []

Checking items scraped from a single start_url, can also be easily achieved using:

  1. $ scrapy parse --spider=myspider -d 3 'http://example.com/page1'

Scrapy Shell

While the parse command is very useful for checking behaviour of a spider, it is of little help to check what happens inside a callback, besides showing the response received and the output. How to debug the situation when parse_details sometimes receives no item?

Fortunately, the shell is your bread and butter in this case (see Invoking the shell from spiders to inspect responses):

  1. from scrapy.shell import inspect_response
  2. def parse_details(self, response, item=None):
  3. if item:
  4. # populate more `item` fields
  5. return item
  6. else:
  7. inspect_response(response, self)

See also: Invoking the shell from spiders to inspect responses.

Open in browser

Sometimes you just want to see how a certain response looks in a browser, you can use the open_in_browser function for that. Here is an example of how you would use it:

  1. from scrapy.utils.response import open_in_browser
  2. def parse_details(self, response):
  3. if "item name" not in response.body:
  4. open_in_browser(response)

open_in_browser will open a browser with the response received by Scrapy at that point, adjusting the base tag so that images and styles are displayed properly.

Logging

Logging is another useful option for getting information about your spider run. Although not as convenient, it comes with the advantage that the logs will be available in all future runs should they be necessary again:

  1. def parse_details(self, response, item=None):
  2. if item:
  3. # populate more `item` fields
  4. return item
  5. else:
  6. self.logger.warning('No item received for %s', response.url)

For more information, check the Logging section.