gpt4 book ai didi

javascript - Puppeteer 无法在 VPS (DigitalOcean) 上工作

转载 作者:太空宇宙 更新时间:2023-11-04 01:40:39 26 4
gpt4 key购买 nike

我在 DigitalOcean 的水滴中我收到此错误。

(node:5549) UnhandledPromiseRejectionWarning: TimeoutError: Navigation Timeout Exceeded: 300000ms exceeded
at Promise.then (/var/www/screenshot/node_modules/puppeteer/lib/NavigatorWatcher.js:94:
at <anonymous>

我尝试截图的网址是https://www.pontofrio.com.br/

我添加了一个用户代理来绕过针对 headless 请求的保护。

它在我的本地计算机上运行,但是当我在我的 VPS 上运行时,即使我增加了超时值,它也会出现错误。

我正在使用 postman 进行测试。

本地计算机 Windows 8.1 x64 - 工作

虚拟机 Linux 16.04 x64 - 工作

VPS Linux 16.04 x64 - 失败

这是代码

const bodyParser = require('body-parser');
const cors = require('cors');
const express = require('express');
const fs = require('fs');
const mkdirp = require('mkdirp');
const path = require('path');
const puppeteer = require('puppeteer');

const PORT = 5005;
const userAgent = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3542.0 Safari/537.36';

express()
.use(cors())
// parse application/x-www-form-urlencoded
.use(bodyParser.urlencoded({ extended: true }))
// parse application/json
.use(bodyParser.json())
.post('/screenshot', (req, res) => {
let result;
const filename = req.body.filename;
const filepath = path.join(__dirname, 'images');
const url = req.body.url;
const wdt = req.body.width || 1366;
const hgt = req.body.height || 768;

if (!fs.existsSync(filepath)) mkdirp.sync(filepath);

(async () => {
const browser = await puppeteer.launch({
executablePath: '/opt/google/chrome/chrome',
args: [
'--disable-setuid-sandbox',
'--disable-gpu',
'--no-first-run',
'--no-sandbox',
]
});
const page = await browser.newPage();

await page.setUserAgent(userAgent);

await page.goto(url, { waitUntil: 'networkidle2', timeout: 300000 });
await page.setViewport({ width: parseInt(wdt), height: parseInt(hgt) });
await page.screenshot({ path: `${filepath}/${filename}.jpg` });

await browser.close();

if (fs.existsSync(path.join(`${filepath}/${filename}.jpg`))) {
result = `Imagem salva em: ${filepath}/${filename}.jpg`;
} else {
result = 'Erro ao salvar imagem, tente novamente.';
}

res.send(result);
})();
})
.use('/screenshot', express.static((path.join(__dirname, '/images'))))
.listen(PORT, () => console.log(`Rodando na porta ${PORT}`));

最佳答案

已解决

问题是 IP 阻塞,我可以使用代理来修复。

现在我使用 --proxy-server 参数,如下所示:

const browser = await puppeteer.launch({
executablePath: '/opt/google/chrome/chrome',
args: [
'--disable-setuid-sandbox',
'--no-sandbox',
'--disable-gpu',
'--no-first-run',
`--proxy-server=${proxyServer}`,
]
});

现在脚本可以运行了!

感谢大家的帮助!

关于javascript - Puppeteer 无法在 VPS (DigitalOcean) 上工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53091884/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com