Puppeteer在VPS(DigitalOcean)上不起作用

时间:2018-10-31 20:59:48

标签: javascript node.js digital-ocean vps puppeteer

我在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/

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

它在我的本地计算机上工作,但是当我以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}`));

1 个答案:

答案 0 :(得分:0)

已解决

问题是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}`,
    ]
});

现在脚本可以工作了!

感谢大家的帮助!