Python Scrapy:无法在Jupyter笔记本脚本中重新运行,报告ReactorNotRestartable
我的剪贴代码如下所示:Python Scrapy:无法在Jupyter笔记本脚本中重新运行,报告ReactorNotRestartable,python,scrapy,jupyter-notebook,Python,Scrapy,Jupyter Notebook,我的剪贴代码如下所示: import scrapy from scrapy.crawler import CrawlerProcess class MovieSpider(scrapy.Spider): name = "movies" start_urls = [ 'https://movie.douban.com/subject/25934014/', 'https://movie.douban.com/subject/25852314/',
import scrapy
from scrapy.crawler import CrawlerProcess
class MovieSpider(scrapy.Spider):
name = "movies"
start_urls = [
'https://movie.douban.com/subject/25934014/',
'https://movie.douban.com/subject/25852314/',
]
def parse(self, response):
title = response.css('div#wrapper div#content h1 span::text').extract_first()
year = response.css('div#wrapper div#content h1 span.year::text').extract_first()
yield {
'url': response.url,
'title': title,
'year': year,
}
我是这样运行的
process = CrawlerProcess({
'USER_AGENT': 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)',
'FEED_FORMAT': 'json',
'FEED_URI': 'movie.json',
'FEED_EXPORT_ENCODING':'utf-8'
})
process.crawl(MovieSpider)
process.start() #
这是推荐的方法
问题是,在运行上述脚本之后,我无法再次运行它。Jupyter笔记本返回错误ReactorNotRestartable
如果我在jupyter中重新启动内核,第一次运行就可以了
我认为问题已经在报告中说明了
我可能通过使用他们的代码来解决这个问题。然而,对于这样一件小事,他们的代码相当复杂,与文档中推荐的CrawlerProcess
方式相去甚远
我想知道是否有更好的方法来解决这个问题
我尝试在脚本末尾添加
process.stop()
。它没有帮助。当使用笔记本电脑时,我相信您必须遵循中描述的方法之一。Twisted使用大量全局文件,这些文件将绑定到笔记本电脑进程中,并且不会被清理。这意味着您只能运行一次爬虫程序,您需要找到另一种运行爬虫程序的方法尝试使用文档中的爬虫程序配方添加reactor.stop()
。我面临同样的问题,我想您应该启动爬虫程序=爬虫程序(您的设置)爬虫程序。start()可能重复