python - 在 Google Cloud Function 中运行 Scrapy 蜘蛛
问题描述
我目前正在尝试让 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 }
有人可以向我解释发生了什么吗?
这可能是我要达到的资源配额吗?
解决方案
这是因为您的云函数不是幂等的。在底层,Scrapy 正在使用 Twisted,它设置了许多全局对象来为其事件驱动的机器提供动力。
https://weautomate.org/articles/running-scrapy-spider-cloud-function/
推荐阅读
- python-3.x - Set redirect URL for Spotify API
- ruby-on-rails - Accessing has_one object across multiple associations/join tables
- angular - Angular 6 - Expression has changed after it was checked
- mysql - 连接具有不同名称的表
- bash - ack 命令没有从 bash 脚本中的当前目录返回输出
- python - Kubernetes REST API call using python
- hyperlink - How can I find out how many times a webpage has been visited on someone else's site?
- javafx - 为什么 TableCell 的 isSelected() 方法在 JavaFX8 TableRow 中向前和向后移动时会给出不同的结果?
- awk - 如何将一列中的数据拆分为新列在linux中使用awk
- mysql - 如果逗号后面的第一个数字是 0,则 MYSQL 浮点值无法更新