【发布时间】:2020-07-02 06:26:07
【问题描述】:
我目前正在尝试让 scrapy 在 Google Cloud Function 中运行。
from flask import escape
from scrapy.crawler import CrawlerProcess
from scrapy.utils.project import get_project_settings
def hello_http(request):
settings = get_project_settings()
process = CrawlerProcess(settings)
process.crawl(BlogSpider)
process.start()
return 'Hello {}!'.format(escape("Word"))
这有效,但奇怪的是,不是“一直”。
每隔一段时间,HTTP 调用就会返回一个错误,然后我可以阅读堆栈驱动程序:
Function execution took 509 ms, finished with status: 'crash'
我检查了蜘蛛,甚至将其简化为不会失败的东西,例如:
import scrapy
class BlogSpider(scrapy.Spider):
name = 'blogspider'
start_urls = ['https://blog.scrapinghub.com']
def parse(self, response):
yield { 'id': 1 }
谁能给我解释一下这是怎么回事?
这可能是我达到的资源配额吗?
【问题讨论】:
-
我没有看到您的第一个代码块与第二个代码块有何关联。云函数在哪里使用
BlogSpider? -
是的,抱歉,这只是一个错字。我修好了。
-
我一直在尝试重现您的问题,据我所知,第一次运行这些功能时,一切都按预期工作。再次执行该函数后,我可以看到以下错误:twisted.internet.error.ReactorNotRestartable。据我所知,这似乎是行不通的,因为每个进程只有一个反应器,你不能启动它两次。我发现你可以尝试实现类似this 的东西。希望对你有帮助
-
我还发现这个 stackoverflow 帖子可能会有所帮助:post-1 和 post-2。根据documentation,当您运行scrapy crawl 时,Scrapy 每个进程运行一个蜘蛛。但是,Scrapy 支持使用内部 API 在每个进程中运行多个蜘蛛。
-
我结束了将我的蜘蛛包裹在它自己的进程中。 @ChristopherRodriguezConde 的解决方案让我走上了正轨。我在这里写过:weautomate.org/articles/running-scrapy-spider-cloud-function
标签: python scrapy google-cloud-functions