2013-03-22 89 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的好像是坏了,如果你添加一些网址的一个列表,并遍历透彻他们并用回调产生请求,它工作正常。