相关疑难解决方法(0)

在Celery任务中运行Scrapy蜘蛛

我有一个Django站点,当用户请求它时会发生刮擦,我的代码在新进程中启动Scrapy蜘蛛独立脚本.当然,这不会增加用户.

像这样的东西:

class StandAloneSpider(Spider):
    #a regular spider

settings.overrides['LOG_ENABLED'] = True
#more settings can be changed...

crawler = CrawlerProcess( settings )
crawler.install()
crawler.configure()

spider = StandAloneSpider()

crawler.crawl( spider )
crawler.start()
Run Code Online (Sandbox Code Playgroud)

我决定使用Celery并使用worker来排队爬网请求.

但是,我遇到了Tornado反应堆无法重启的问题.第一个和第二个蜘蛛成功运行,但后续的蜘蛛会抛出ReactorNotRestartable错误.

任何人都可以在Celery框架内分享运行蜘蛛的任何提示?

python django scrapy celery

33
推荐指数
2
解决办法
1万
查看次数

在Python中运行Scrapy任务

当我在命令行的"一次性"场景中运行它时,我的Scrapy脚本似乎工作正常,但如果我尝试在同一个python会话中运行代码两次,我会收到此错误:

"ReactorNotRestartable"

为什么?

违规代码(最后一行抛出错误):

crawler = CrawlerProcess(settings)
crawler.install()
crawler.configure()

# schedule spider
#crawler.crawl(MySpider())
spider = MySpider()
crawler.queue.append_spider(spider)

# start engine scrapy/twisted
crawler.start()
Run Code Online (Sandbox Code Playgroud)

python scrapy

10
推荐指数
1
解决办法
8986
查看次数

运行scrapy crawler的最简单方法,这样它就不会阻塞脚本

官方文档提供了许多scrapy从代码运行爬虫的方法:

import scrapy
from scrapy.crawler import CrawlerProcess

class MySpider(scrapy.Spider):
    # Your spider definition
    ...

process = CrawlerProcess({
    'USER_AGENT': 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)'
})

process.crawl(MySpider)
process.start() # the script will block here until the crawling is finished
Run Code Online (Sandbox Code Playgroud)

但它们都阻止脚本,直到爬行完成。python中以非阻塞、异步方式运行爬虫的最简单方法是什么?

python scrapy

3
推荐指数
1
解决办法
2371
查看次数

标签 统计

python ×3

scrapy ×3

celery ×1

django ×1