您不能重新启动反应堆,但是应该可以通过分叉一个单独的过程来使其运行更多次:
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.”...


![Scrapy-Reactor无法重新启动[重复] Scrapy-Reactor无法重新启动[重复]](http://www.mshxw.com/aiimages/31/517148.png)
