2014-10-04 37 views
2

我是Scrapy的新手,如果這個問題微不足道,我很抱歉。我從官方網頁上閱讀了Scrapy的文檔。而我通過看文件,我遇到了這個例子:python scrapy parse()函數,其中返回的值是什麼?

import scrapy 
from myproject.items import MyItem 

class MySpider(scrapy.Spider): 
    name = ’example.com’ 
    allowed_domains = [’example.com’] 
    start_urls = [ 
    ’http://www.example.com/1.html’, 
    ’http://www.example.com/2.html’, 
    ’http://www.example.com/3.html’, 
    ] 

    def parse(self, response): 
    for h3 in response.xpath(’//h3’).extract(): 
     yield MyItem(title=h3) 
    for url in response.xpath(’//a/@href’).extract(): 
     yield scrapy.Request(url, callback=self.parse) 

我知道,解析方法必須返回一個項目或/和要求,但如果在這些返回值返回?

一個是一個項目,另一個是請求,我認爲這兩個類型的處理方式不同,在CrawlSpider的情況下,它具有回調規則。這個回調的返回值呢?去哪兒 ?與parse()相同?

我對Scrapy過程很困惑,甚至我所讀取的原稿....

回答

8

按照documentation

的parse()方法是負責處理響應和 的返回刮取的數據(作爲Item對象)以及更多URL(如 請求對象)。

換句話說,返回/產生的項目和請求處理方式不同,項目被交給項目管道和項目出口商,但請求被放入Scheduler該管的請求到Downloader用於製作請求並返回響應。然後,引擎接收到響應並將其提供給蜘蛛進行處理(至callback方法)。

整個數據流過程是在Architecture Overview頁面以非常詳細的方式描述。

希望有所幫助。