简体   繁体   English

跟随链接时,Python scrapy请求未执行回调

[英]Python scrapy Request not executing callback when following links

I have created the following python file named test.py : 我创建了以下名为test.py python文件:

import scrapy


class EventsSpider(scrapy.Spider):
name = "test"
start_urls = [
    'https://www.granteatrocc.com/programacion.php',
]

    def parse(self, response):
        print "\nBEFORE-------------------------------------------------\n"
        scrapy.Request("https://stackoverflow.com/questions/6453269/jquery-select-element-by-xpath", callback=self.parse_two)
        print "\nAFTER-------------------------------------------------\n"

    def parse_two(self, response):
        print "THIS IS WORKING"

And I Execute it using the following command: 然后使用以下命令执行它:

scrapy runspider test.py

Obviously this example is a simplified version of a Crawler that must follow links to get extra fields. 显然,此示例是Crawler的简化版本,必须遵循链接才能获取更多字段。 The output I get is the following. 我得到的输出如下。 As you can see, the string "THIS IS WORKING" is never printed, so I understand that ````parse_two function is never called: 如您所见,从未打印过字符串“ THIS IS WORKING” ,因此我了解到``永远不会调用parse_two函数:

Jesuss-MacBook-Pro:tutorial jesusredondogarcia$ scrapy runspider test.py
2017-10-24 10:19:09 [scrapy.utils.log] INFO: Scrapy 1.4.0 started (bot: scrapybot)
2017-10-24 10:19:09 [scrapy.utils.log] INFO: Overridden settings: {'SPIDER_LOADER_WARN_ONLY': True}
2017-10-24 10:19:09 [scrapy.middleware] INFO: Enabled extensions:
['scrapy.extensions.memusage.MemoryUsage',
 'scrapy.extensions.logstats.LogStats',
 'scrapy.extensions.telnet.TelnetConsole',
 'scrapy.extensions.corestats.CoreStats']
2017-10-24 10:19:09 [scrapy.middleware] INFO: Enabled downloader middlewares:
['scrapy.downloadermiddlewares.httpauth.HttpAuthMiddleware',
 'scrapy.downloadermiddlewares.downloadtimeout.DownloadTimeoutMiddleware',
 'scrapy.downloadermiddlewares.defaultheaders.DefaultHeadersMiddleware',
 'scrapy.downloadermiddlewares.useragent.UserAgentMiddleware',
 'scrapy.downloadermiddlewares.retry.RetryMiddleware',
 'scrapy.downloadermiddlewares.redirect.MetaRefreshMiddleware',
 'scrapy.downloadermiddlewares.httpcompression.HttpCompressionMiddleware',
 'scrapy.downloadermiddlewares.redirect.RedirectMiddleware',
 'scrapy.downloadermiddlewares.cookies.CookiesMiddleware',
 'scrapy.downloadermiddlewares.httpproxy.HttpProxyMiddleware',
 'scrapy.downloadermiddlewares.stats.DownloaderStats']
2017-10-24 10:19:09 [scrapy.middleware] INFO: Enabled spider middlewares:
['scrapy.spidermiddlewares.httperror.HttpErrorMiddleware',
 'scrapy.spidermiddlewares.offsite.OffsiteMiddleware',
 'scrapy.spidermiddlewares.referer.RefererMiddleware',
 'scrapy.spidermiddlewares.urllength.UrlLengthMiddleware',
 'scrapy.spidermiddlewares.depth.DepthMiddleware']
2017-10-24 10:19:09 [scrapy.middleware] INFO: Enabled item pipelines:
[]
2017-10-24 10:19:09 [scrapy.core.engine] INFO: Spider opened
2017-10-24 10:19:09 [scrapy.extensions.logstats] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
2017-10-24 10:19:09 [scrapy.extensions.telnet] DEBUG: Telnet console listening on 127.0.0.1:6023
2017-10-24 10:19:10 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://www.granteatrocc.com/programacion.php> (referer: None)
BEFORE-------------------------------------------------
AFTER-------------------------------------------------
2017-10-24 10:19:10 [scrapy.core.engine] INFO: Closing spider (finished)
2017-10-24 10:19:10 [scrapy.statscollectors] INFO: Dumping Scrapy stats:
{'downloader/request_bytes': 234,
 'downloader/request_count': 1,
 'downloader/request_method_count/GET': 1,
 'downloader/response_bytes': 9029,
 'downloader/response_count': 1,
 'downloader/response_status_count/200': 1,
 'finish_reason': 'finished',
 'finish_time': datetime.datetime(2017, 10, 24, 8, 19, 10, 542732),
 'log_count/DEBUG': 2,
 'log_count/INFO': 7,
 'memusage/max': 46268416,
 'memusage/startup': 46268416,
 'response_received_count': 1,
 'scheduler/dequeued': 1,
 'scheduler/dequeued/memory': 1,
 'scheduler/enqueued': 1,
 'scheduler/enqueued/memory': 1,
 'start_time': datetime.datetime(2017, 10, 24, 8, 19, 9, 237051)}
2017-10-24 10:19:10 [scrapy.core.engine] INFO: Spider closed (finished)

IIRC,在评估就地Request ,解析方法应使用yield关键字

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM