python - Scrapy:非阻塞暂停

标签 python scrapy scrapy-spider

我有问题。我需要停止执行一个函数一段时间,但不要停止整个解析的执行。也就是说,我需要一个非阻塞的暂停。

看起来像:

class ScrapySpider(Spider):
    name = 'live_function'

    def start_requests(self):
        yield Request('some url', callback=self.non_stop_function)

    def non_stop_function(self, response):
        for url in ['url1', 'url2', 'url3', 'more urls']:
            yield Request(url, callback=self.second_parse_function)

        # Here I need some function for sleep only this function like time.sleep(10)

        yield Request('some url', callback=self.non_stop_function)  # Call itself

    def second_parse_function(self, response):
        pass

函数 non_stop_function 需要停止一段时间,但它不应该阻塞其余的输出。

如果我插入 time.sleep() - 它会停止整个解析器,但我不需要它。是否可以使用 twisted 或其他方式停止一个功能?

原因:我需要创建一个非阻塞函数,每隔 n 秒解析一次网站的页面。在那里,她将获得网址并填写 10 秒。已经获取到的网址会继续工作,但主要功能需要休眠。

更新:

感谢 TkTechviach。一个答案帮助我了解了如何制作待处理的Request,第二个是如何激活它。两个答案相得益彰,我为 Scrapy 做了一个很好的非阻塞暂停:

def call_after_pause(self, response):
    d = Deferred()
    reactor.callLater(10.0, d.callback, Request(
        'https://example.com/',
        callback=self.non_stop_function,
        dont_filter=True))
    return d

并为我的请求使用此功能:

yield Request('https://example.com/', callback=self.call_after_pause, dont_filter=True)

最佳答案

Request 对象有 callback 参数,尝试使用该参数。 我的意思是,创建一个包装 self.second_parse_functionpauseDeferred

这是我的脏且未经测试的示例,已标记更改的行。

class ScrapySpider(Spider):
    name = 'live_function'

    def start_requests(self):
        yield Request('some url', callback=self.non_stop_function)

    def non_stop_function(self, response):

        parse_and_pause = Deferred()  # changed
        parse_and_pause.addCallback(self.second_parse_function) # changed
        parse_and_pause.addCallback(pause, seconds=10)  # changed

        for url in ['url1', 'url2', 'url3', 'more urls']:
            yield Request(url, callback=parse_and_pause)  # changed

        yield Request('some url', callback=self.non_stop_function)  # Call itself

    def second_parse_function(self, response):
        pass

如果该方法适合您,那么您可以创建一个函数,该函数根据规则构造一个 Deferred 对象。它可以通过如下方式实现:

def get_perform_and_pause_deferred(seconds, fn, *args, **kwargs):
    d = Deferred()
    d.addCallback(fn, *args, **kwargs)
    d.addCallback(pause, seconds=seconds)
    return d

这是可能的用法:

class ScrapySpider(Spider):
    name = 'live_function'

    def start_requests(self):
        yield Request('some url', callback=self.non_stop_function)

    def non_stop_function(self, response):
        for url in ['url1', 'url2', 'url3', 'more urls']:
            # changed
            yield Request(url, callback=get_perform_and_pause_deferred(10, self.second_parse_function))

        yield Request('some url', callback=self.non_stop_function)  # Call itself

    def second_parse_function(self, response):
        pass

关于python - Scrapy:非阻塞暂停,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36984696/

相关文章:

python - 尝试按城市将数据从 csv 导出到文本

python - PySide:创建复选框列表

Python 设置并行端口数据引脚高/低

python - Scrapy 获取错误为 "DNS lookup failed"的网站

Scrapyd 404错误运行curl http ://localhost:6800/daemonstatus. json

python - 未绑定(bind)本地错误 : local variable 'a' referenced before assignment

python - 如何处理未由 http 状态代码指示的临时错误?

python - Scrapy - 连接以非干净的方式丢失。跨单个域不一致

python - XPATH 选择器无法选择 html 代码块

python - 无法在 Scrapy 蜘蛛中使用多个代理