Search code examples
yieldscrapy

Scrapy: How to implement of yield in 3rd party function?


The code looks like following:

def parse(self, response):
    param = {}
    self.send_request(self, param)

def send_request(self, param):
    url = "www.sample.com/auto/"
    yield FormRequest(url, callback=self.parse_auto, formdata=param, method="POST")

def parse_auto(self, response):
    ...

Why yield does not work in this code? I would like to reuse the send_request in other sections.

Log:

2017-02-26 23:43:16 [scrapy.middleware] INFO: Enabled extensions:
['scrapy.extensions.feedexport.FeedExporter',
 'scrapy.extensions.logstats.LogStats',
 'scrapy.extensions.telnet.TelnetConsole',
 'scrapy.extensions.corestats.CoreStats']
2017-02-26 23:43:16 [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.stats.DownloaderStats']
2017-02-26 23:43:16 [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-02-26 23:43:16 [scrapy.middleware] INFO: Enabled item pipelines:
[]
2017-02-26 23:43:16 [scrapy.core.engine] INFO: Spider opened
2017-02-26 23:43:16 [scrapy.extensions.logstats] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
2017-02-26 23:43:16 [scrapy.extensions.telnet] DEBUG: Telnet console listening on 127.0.0.1:6023
2017-02-26 23:43:17 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://www.compreseguros.com/> (referer: None)
2017-02-26 23:43:18 [scrapy.core.engine] INFO: Closing spider (finished)
2017-02-26 23:43:18 [scrapy.statscollectors] INFO: Dumping Scrapy stats:
{'downloader/request_bytes': 291,
 'downloader/request_count': 1,
 'downloader/request_method_count/GET': 1,
 'downloader/response_bytes': 7561,
 'downloader/response_count': 1,
 'downloader/response_status_count/200': 1,
 'finish_reason': 'finished',
 'finish_time': datetime.datetime(2017, 2, 26, 15, 43, 18, 32000),
 'log_count/DEBUG': 2,
 'log_count/INFO': 7,
 'response_received_count': 1,
 'scheduler/dequeued': 1,
 'scheduler/dequeued/memory': 1,
 'scheduler/enqueued': 1,
 'scheduler/enqueued/memory': 1,
 'start_time': datetime.datetime(2017, 2, 26, 15, 43, 16, 355000)}
2017-02-26 23:43:18 [scrapy.core.engine] INFO: Spider closed (finished)

This is log result. Look at the sent_request function body. There is a change.


Solution

  • Here is fully working code.

    All you needed is to return than method from parse

    # -*- coding: utf-8 -*-
    import scrapy, logging
    from scrapy.http import FormRequest, Request
    
        class Test1SpiderSpider(scrapy.Spider):
            name = "test1_spider"
    
            start_urls  = ["http://stackoverflow.com"]
    
            def parse(self, response):
                param = {}
                return self.send_request(param)
    
            def send_request(self, param):
                logging.info("send_request is called")
                url = "http://stackoverflow.com"
                yield FormRequest(url, callback=self.parse_auto, formdata=param, method="POST")
    
            def parse_auto(self, response):
                logging.info("parse_auto is called")