繁体   English   中英

如何使用 Puppeteer 自动截取多个页面的屏幕截图

[英]How to take screenshots for multiple pages automatically using Puppeteer

我在文本文件中有一个 URL 列表。 我想通过使用 Puppeteer 从文本文件中读取每个 URL 来自动获取每个页面的屏幕截图。

const puppeteer = require('puppeteer');
async function doScreenCapture(url, site_name) {
    let browser = await puppeteer.launch({ headless: false });
    let page = await browser.newPage();
    await page.goto(url);
    await page.setViewport({width: 1382, height: 717})
    await page.waitFor(1000);
    console.log('do screen capture running');
    await page.screenshot({ path:`${site_name}.png`, fullPage: true });
    await page.close();
    await browser.close();
  }
async function run() {
    console.log('running');
    var fs = require("fs");
    var text = fs.readFileSync("linksList.txt").toString().split("\n");

    for (var i = 0; i < text.length; ++i) {
            doScreenCapture(text[i], "image"+i)
            console.log("image"+i+" completed");
            await page.waitFor(5000);
        }
 }

run();

这是我在运行代码时遇到的错误

(node:77868) UnhandledPromiseRejectionWarning: ReferenceError: page is not defined at run at Object. 在 Module._compile (module.js:653:30) at Object.Module._extensions..js (module.js:664:10) at Module.load (module.js:566:32) at tryModuleLoad (module.js) :506:12) at Function.Module._load (module.js:498:3) at Function.Module.runMain (module.js:694:10) at bootstrap_node.js (bootstrap_node.js:204:16) at bootstrap_node.js :625:3 (node:77868) UnhandledPromiseRejectionWarning:未处理的承诺拒绝。 这个错误要么是因为在没有 catch 块的情况下抛出了异步函数,要么是因为拒绝了一个没有用 .catch() 处理过的承诺。 (rejection id: 1) (node:77868) [DEP0018] DeprecationWarning:不推荐使用未处理的承诺拒绝。 将来,未处理的承诺拒绝将使用非零退出代码终止 Node.js 进程。 做屏幕截图运行。

我在这里看到两个问题:

  • page不存在于“for”循环中,只存在于“run”函数中,这给了你例外。 您可以移动等待功能

  • “doScreenCapture”是一个异步函数,您应该使用await doScreenCapture以便连续打开页面,而不是一次打开。

暂无
暂无

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

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