简体   繁体   English

为什么 puppeteer 在 azure webapp 上不起作用

[英]Why does puppeteer not work on azure webapp

I have used Nodejs and try to takes the screenshot of given URL.我使用过 Nodejs 并尝试截取给定 URL 的屏幕截图。 It works perfect on local but After deploy to Azure have problems on create puppeteer.它在本地运行完美,但部署到 Azure 后在创建 puppeteer 时出现问题。

Code代码

exports.getScreenShot = async function (req, res) {
    const requesturl = req.param('url');
    if (!requesturl) {
        return res.send(400, 'Missing url');
    }
    const parsedUrl = url.parse(requesturl);
    if (!parsedUrl.protocol) {
        return res.send(400, 'Invalid url, missing protocol');
    }
    if (!parsedUrl.hostname) {
        return res.send(400, 'Invalid url, missing hostname');
    }

    const options = {
        'width': req.param('width'),
        'height': req.param('height'),
        'delay': req.param('delay'),
        'userAgent': req.param('userAgent'),
        'full': (req.param('full') === 'true')
    };

    options.width = options.width || 1024;
    options.height = options.height || 768;
    options.delay = options.delay || 200;    
    const browser = await puppeteer.connect({
        browserWSEndpoint: 'wss://chrome.browserless.io/'
    });

    let page = await browser.newPage();
    await page.goto(requesturl);
    await page.waitFor(parseInt(options.delay));
    let imageName = parsedUrl.hostname.replace(/\W/g, '_');
    let pathName = parsedUrl.pathname.replace(/\W/g, '_').replace(/_$/, '');
    if (pathName) {
        imageName += pathName;
    }
    imageName = `${imageName}.png`;
    var tempPath = temp.path({ suffix: '.png' });
    await page.setViewport({ width: parseInt(options.width), height: parseInt(options.height) });
    await page.screenshot({
        path: tempPath,
        fullPage: options.full
    });
}

Refer to the SO Tag Info for puppeteer , as below.请参阅puppeteer的 SO 标签信息,如下所示。

Puppeteer is a Node library which provides a high-level API to control headless Chrome or Chromium over the DevTools Protocol. Puppeteer 是一个 Node 库,它提供了一个高级 API 来通过 DevTools 协议控制无头 ChromeChromium It can also be configured to use full (non-headless) Chrome or Chromium .它还可以配置为使用完整(非无头) Chrome 或 Chromium

Whatever Chrome or Chromium with headless or non-headless, they all require GDI support.无论是无头或非无头的 Chrome 或 Chromium,它们都需要 GDI 支持。 However, on Azure App Services on Windows, it conflicts with Win32k.sys (User32/GDI32) Restrictions , as the figure below.但是,在 Windows 上的 Azure 应用服务上,它与Win32k.sys (User32/GDI32) Restrictions冲突,如下图。

在此处输入图片说明

And other frameworks like PhantomJS/Selenium also be restricted by it, see below.而其他框架如PhantomJS/Selenium也受到它的限制,见下文。

在此处输入图片说明

So you can not use puppeteer within Azure WebApp on Windows.所以你不能在 Windows 上的 Azure WebApp 中使用 puppeteer。 The workaround solution is to use Azure VM or Azure WebApp on Linux.解决方法是在 Linux 上使用 Azure VM 或 Azure WebApp。 Essentially, this issue is duplicated with your other SO thread Chrome driver is not working on azure web apps .从本质上讲,此问题与您的其他 SO 线程Chrome 驱动程序无法在 azure web 应用程序上工作重复。

Hope it helps.希望能帮助到你。

How to run Headless Chrome in Azure Cloud Service or Azure Functions 如何在 Azure 云服务或 Azure Functions 中运行 Headless Chrome

Replace puppeteer.launch with puppeteer.connect用 puppeteer.connect 替换 puppeteer.launch

const browser = await puppeteer.connect({  browserWSEndpoint: 'wss://chrome.browserless.io/'

}); });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM