smi*_*007 15 node.js google-cloud-functions puppeteer
我在 GCF 中的设置:
npm install --save puppeteer
从项目云 shell安装
像这样编辑 package.json :
{ "dependencies": { "puppeteer": "^19.2.2" } }
将medium.com中的代码粘贴到index.js中: https://gist.githubusercontent.com/Alezco/b9b7ce4ec7ee7f208818e395225fcbbe/raw/8554acc8b311a10e272f5d1b98dce3400945bb00/index.js
使用 2 GB RAM 部署,0-3 个实例,最长 500 秒超时
构建或打开 URL 后出现以下错误:
npm install
)或 2. 您的缓存路径配置不正确(即:/workspace/.cache/puppeteer)。对于 (2),请查看我们有关配置 puppeteer 的指南:https://pptr.dev/guides/configuration。当我运行时,npm list
webdriver 和 puppeteer 都已安装。我怀疑这条路径有问题,但我不知道它应该通向哪里。executablePath
然后我可以为 puppeteer.launch() 提供可能解决问题的参数。我尝试重新安装 puppeteer 并更改配置。没有运气。
小智 4
以下示例在使用Node.js 16 的Cloud Functions Gen 2上运行(我未能使 Node.js 18 正常工作)。
具有 puppeteer 功能的 JS 文件:
const puppeteer = require('puppeteer')
let browserPromise = puppeteer.launch(
{
args: [
'--no-sandbox'
]
}
);
exports.productads = async (req, res) => {
/* Your function goes here*/
}
Run Code Online (Sandbox Code Playgroud)
你需要有.puppeteerrc.cjs:
const {join} = require('path');
module.exports = {
cacheDirectory: join(__dirname, '.cache', 'puppeteer')
};
Run Code Online (Sandbox Code Playgroud)
和package.json类似:
{
"name": "puppeteer",
"version": "1.0.0"
"description": "",
"main": "index.js",
"scripts": {
"gcp-build": "node node_modules/puppeteer/install.js"
},
"devDependencies": {
"@google-cloud/functions-framework": "^3.1.2"
},
"dependencies": {
"puppeteer": "^19.2.2"
}
}
Run Code Online (Sandbox Code Playgroud)
这两个文件都应放置在 .js 文件旁边: 参见图片
归档时间: |
|
查看次数: |
10724 次 |
最近记录: |