2013-03-22 90 views
2

我試圖用Scrapy做遞歸解析腳本,但Request()函數不調用回調函數suppose_to_parse(),也沒有提供回調函數中的任何函數。我嘗試了不同的變化,但他們都沒有工作。在哪裏挖?scrapy無法發出請求()回調

from scrapy.http import Request 
from scrapy.spider import BaseSpider 
from scrapy.selector import HtmlXPathSelector 



class joomler(BaseSpider): 
    name = "scrapy" 
    allowed_domains = ["scrapy.org"] 
    start_urls = ["http://blog.scrapy.org/"] 


    def parse(self, response): 
     print "Working... "+response.url 
     hxs = HtmlXPathSelector(response) 
     for link in hxs.select('//a/@href').extract(): 
      if not link.startswith('http://') and not link.startswith('#'): 
       url="" 
       url=(self.start_urls[0]+link).replace('//','/') 
       print url 
       yield Request(url, callback=self.suppose_to_parse) 


    def suppose_to_parse(self, response): 
     print "asdasd" 
     print response.url 

回答

1

移動產量if聲明之外:

for link in hxs.select('//a/@href').extract(): 
    url = link 
    if not link.startswith('http://') and not link.startswith('#'): 
     url = (self.start_urls[0] + link).replace('//','/') 

    print url 
    yield Request(url, callback=self.suppose_to_parse) 
1

我不是專家,但我想你的代碼,我認爲這個問題是不是請求,生成的URL的好像是壞了,如果你添加一些網址的一個列表,並遍歷透徹他們並用回調產生請求,它工作正常。