擅长:python、mysql、java
<p>在代码示例中,调用扭曲电抗器在每次函数调用时启动它。这不起作用,因为每个进程只有一个反应器,而您不能<a href="https://stackoverflow.com/questions/18910912/reactornotrestartable-when-launching-two-equivalent-unittest-with-twisted-and-tr">start it twice</a>。在</p>
<p>有两种解决问题的方法,都在<a href="http://doc.scrapy.org/en/latest/topics/practices.html#running-multiple-spiders-in-the-same-process" rel="nofollow noreferrer">documentation here</a>中描述。要么坚持使用<code>CrawlerRunner</code>,但将<code>reactor.run()</code>移到<code>search()</code>函数之外,以确保它只被调用一次。或者使用<code>CrawlerProcess</code>并简单地调用<code>crawler_process.start()</code>。第二种方法更简单,您的代码如下所示:</p>
<pre><code>from scrapy.crawler import CrawlerProcess
from dirbot.spiders.dmoz import DmozSpider
def search(runner, keyword):
return runner.crawl(DmozSpider, keyword)
runner = CrawlerProcess()
search(runner, "alfa")
search(runner, "beta")
runner.start()
</code></pre>