栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Scrapy-Reactor无法重新启动[重复]

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Scrapy-Reactor无法重新启动[重复]

您不能重新启动反应堆,但是应该可以通过分叉一个单独的过程来使其运行更多次:

import scrapyimport scrapy.crawler as crawlerfrom multiprocessing import Process, Queuefrom twisted.internet import reactor# your spiderclass QuotesSpider(scrapy.Spider):    name = "quotes"    start_urls = ['http://quotes.toscrape.com/tag/humor/']    def parse(self, response):        for quote in response.css('div.quote'): print(quote.css('span.text::text').extract_first())# the wrapper to make it run more timesdef run_spider(spider):    def f(q):        try: runner = crawler.CrawlerRunner() deferred = runner.crawl(spider) deferred.addBoth(lambda _: reactor.stop()) reactor.run() q.put(None)        except Exception as e: q.put(e)    q = Queue()    p = Process(target=f, args=(q,))    p.start()    result = q.get()    p.join()    if result is not None:        raise result

运行两次:

print('first run:')run_spider(QuotesSpider)print('nsecond run:')run_spider(QuotesSpider)

结果:

first run:“The person, be it gentleman or lady, who has not pleasure in a good novel, must be intolerably stupid.”“A day without sunshine is like, you know, night.”...second run:“The person, be it gentleman or lady, who has not pleasure in a good novel, must be intolerably stupid.”“A day without sunshine is like, you know, night.”...


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/517148.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号