【问题标题】:Puppeteer doesn't work at VPS (DigitalOcean)Puppeteer 不适用于 VPS (DigitalOcean)
【发布时间】:2019-04-05 02:56:56
【问题描述】:

我在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}`));

【问题讨论】:

  • 嗨,欢迎来到 SO。您是否尝试从 DigitalOcean VPS ping 或 cURL 指定的 URI?
  • 看起来您的 VPS IP 确实被目标站点阻止了。
  • @AndréDS 现在尝试了,在我的本地机器上它返回:“bytes=32 23.74.205.72”,在我的 VPS 上它返回“bytes=64 a104-88-22-57.deploy.static .akamaitechnologies.com"
  • 你能告诉我们完整的 ping 输出吗?

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


【解决方案1】:

已解决

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

现在脚本可以运行了!

感谢大家的帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-15
    相关资源
    最近更新 更多