Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding callbacks in Scrapy

I am new to Python and Scrapy. I have not used callback functions before. However, I do now for the code below. The first request will be executed and the response of that will be sent to the callback function defined as second argument:

def parse_page1(self, response):
    item = MyItem()
    item['main_url'] = response.url
    request = Request("http://www.example.com/some_page.html",
                      callback=self.parse_page2)
    request.meta['item'] = item
    return request

def parse_page2(self, response):
    item = response.meta['item']
    item['other_url'] = response.url
    return item

I am unable to understand following things:

  1. How is the item populated?
  2. Does the request.meta line executes before the response.meta line in parse_page2?
  3. Where is the returned item from parse_page2 going?
  4. What is the need of the return request statement in parse_page1? I thought the extracted items need to be returned from here.
like image 479
Parag Avatar asked Mar 25 '14 23:03

Parag


2 Answers

Read the docs:

For spiders, the scraping cycle goes through something like this:

  1. You start by generating the initial Requests to crawl the first URLs, and specify a callback function to be called with the response downloaded from those requests.

    The first requests to perform are obtained by calling the start_requests() method which (by default) generates Request for the URLs specified in the start_urls and the parse method as callback function for the Requests.

  2. In the callback function, you parse the response (web page) and return either Item objects, Request objects, or an iterable of both. Those Requests will also contain a callback (maybe the same) and will then be downloaded by Scrapy and then their response handled by the specified callback.

  3. In callback functions, you parse the page contents, typically using Selectors (but you can also use BeautifulSoup, lxml or whatever mechanism you prefer) and generate items with the parsed data.

  4. Finally, the items returned from the spider will be typically persisted to a database (in some Item Pipeline) or written to a file using Feed exports.

Answers:

How is the 'item' populated does the request.meta line executes before response.meta line in parse_page2?

Spiders are managed by Scrapy engine. It first makes requests from URLs specified in start_urls and passes them to a downloader. When downloading finishes callback specified in the request is called. If the callback returns another request, the same thing is repeated. If the callback returns an Item, the item is passed to a pipeline to save the scraped data.

Where is the returned item from parse_page2 going?

What is the need of return request statement in parse_page1? I thought the extracted items need to be returned from here ?

As stated in the docs, each callback (both parse_page1 and parse_page2) can return either a Request or an Item (or an iterable of them). parse_page1 returns a Request not the Item, because additional info needs to be scraped from additional URL. Second callback parse_page2 returns an item, because all the info is scraped and ready to be passed to a pipeline.

like image 130
warvariuc Avatar answered Oct 27 '22 22:10

warvariuc


  1. yes, scrapy uses a twisted reactor to call spider functions, hence using a single loop with a single thread ensures that
  2. the spider function caller expects to either get item/s or request/s in return, requests are put in a queue for future processing and items are sent to configured pipelines
  3. saving an item (or any other data) in request meta makes sense only if it is needed for further processing upon getting a response, otherwise it is obviously better to simply return it from parse_page1 and avoid the extra http request call
like image 1
Guy Gavriely Avatar answered Oct 28 '22 00:10

Guy Gavriely