Puppeteer 不在 VPS (DigitalOcean) 工作
Puppeteer doesn't work at VPS (DigitalOcean)
我在 DigitalOcean 的 droplet 中,我收到了这个错误。
(node:5549) UnhandledPromiseRejectionWarning: TimeoutError: Navigation Timeout Exceeded: 300000ms exceeded
at Promise.then (/var/www/screenshot/node_modules/puppeteer/lib/NavigatorWatcher.js:94:
at <anonymous>
我要截屏的 url 是 https://www.pontofrio.com.br/
我添加了一个用户代理来绕过针对无头请求的保护。
它在我的本地机器上工作但是当我在我的 VPS 上 运行 它得到错误,即使我增加了超时值。
我正在用邮递员进行测试。
本地计算机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}`,
]
});
现在脚本可以运行了!
感谢大家的帮助!
我在 DigitalOcean 的 droplet 中,我收到了这个错误。
(node:5549) UnhandledPromiseRejectionWarning: TimeoutError: Navigation Timeout Exceeded: 300000ms exceeded
at Promise.then (/var/www/screenshot/node_modules/puppeteer/lib/NavigatorWatcher.js:94:
at <anonymous>
我要截屏的 url 是 https://www.pontofrio.com.br/
我添加了一个用户代理来绕过针对无头请求的保护。
它在我的本地机器上工作但是当我在我的 VPS 上 运行 它得到错误,即使我增加了超时值。
我正在用邮递员进行测试。
本地计算机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}`,
]
});
现在脚本可以运行了!
感谢大家的帮助!