在 Google Cloud Function 中运行 Scrapy 蜘蛛

TKr*_*ugg 5 python scrapy google-cloud-functions

我目前正在尝试让 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"))
Run Code Online (Sandbox Code Playgroud)

这有效,但奇怪的是,不是“一直”。每隔一段时间,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 }
Run Code Online (Sandbox Code Playgroud)

有人可以向我解释一下发生了什么事吗?

这可能是我达到的资源配额吗?

在此输入图像描述

小智 4

这是因为您的 Cloud Function 不是幂等的。在幕后,Scrapy 正在使用 Twisted,它设置了许多全局对象来为其事件驱动机制提供动力。

https://weautomate.org/articles/running-scrapy-spider-cloud-function/