首页 > 解决方案 > 在 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 }

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

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

在此处输入图像描述

标签: pythonscrapygoogle-cloud-functions

解决方案


这是因为您的云函数不是幂等的。在底层,Scrapy 正在使用 Twisted,它设置了许多全局对象来为其事件驱动的机器提供动力。

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


推荐阅读