scrapy无法进行Request()回调

时间:2013-03-22 20:01:03

标签: python scrapy

我正在尝试使用Scrapy创建递归解析脚本,但Request()函数不调用回调函数suppose_to_parse(),也不调用回调值中提供的任何函数。我尝试了不同的变化,但没有一个工作。在哪里挖?

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

2 个答案:

答案 0 :(得分:1)

我不是专家,但我尝试了您的代码,我认为问题不在请求上,如果您添加了一些网址,则生成的网址似乎被破坏了一个列表并迭代它们并通过回调产生Request,它工作正常。

答案 1 :(得分: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)
相关问题