【问题标题】:scrapy unable to make Request() callbackscrapy 无法进行 Request() 回调
【发布时间】:2013-03-22 20:01:03
【问题描述】:

我正在尝试使用 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

【问题讨论】:

    标签: python scrapy


    【解决方案1】:

    将 yield 移到 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)
    

    【讨论】:

      【解决方案2】:

      我不是专家,但我尝试了您的代码,我认为问题不在请求上,如果您将一些 url 添加到列表中并遍历它们并产生请求,生成的 url 似乎已损坏使用回调,它工作正常。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-04-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多