Puppeteer doesn't work at VPS (DigitalOcean)

大城市里の小女人 提交于 2021-01-27 22:53:53

问题


I'm in a droplet at DigitalOcean and i'm getting this error.

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

The url that I'm trying to screenshot is https://www.pontofrio.com.br/

I added an user agent to bypass the protection against headless request.

It worked in my local machine but when I run at my VPS it get the error, even if I increase the timeout value.

I'm testing with postman.

Local Machine Windows 8.1 x64 - Working

Virtual Machine Linux 16.04 x64 - Working

VPS Linux 16.04 x64 - Failing

Here's the code

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:


SOLVED

The problem was an IP block, which I can fix using proxy.

Now I use --proxy-server argument like this:

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

And now the script works!

Thanks everyone for the help!



来源:https://stackoverflow.com/questions/53091884/puppeteer-doesnt-work-at-vps-digitalocean

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!