[英]How to take screenshots for multiple pages automatically using Puppeteer
I have a list of URLs in the text file.我在文本文件中有一个 URL 列表。 I want to get the screenshot for each page automatically by reading each URL form the text file using Puppeteer.
我想通过使用 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();
This is the error I got while running the code这是我在运行代码时遇到的错误
(node:77868) UnhandledPromiseRejectionWarning: ReferenceError: page is not defined at run at Object. (node:77868) UnhandledPromiseRejectionWarning: ReferenceError: page is not defined at run at Object. at 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 startup (bootstrap_node.js:204:16) at bootstrap_node.js:625:3 (node:77868) UnhandledPromiseRejectionWarning: Unhandled promise rejection.
在 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:未处理的承诺拒绝。 This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch().
这个错误要么是因为在没有 catch 块的情况下抛出了异步函数,要么是因为拒绝了一个没有用 .catch() 处理过的承诺。 (rejection id: 1) (node:77868) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated.
(rejection id: 1) (node:77868) [DEP0018] DeprecationWarning:不推荐使用未处理的承诺拒绝。 In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
将来,未处理的承诺拒绝将使用非零退出代码终止 Node.js 进程。 do screen capture running.
做屏幕截图运行。
I see two issues here:我在这里看到两个问题:
page
does not exist in the "for" loop, only in the "run" function and that gives you the exception. page
不存在于“for”循环中,只存在于“run”函数中,这给了你例外。 You can move waiting to the function您可以移动等待功能
"doScreenCapture" is an async function and you should use await doScreenCapture
in order for pages to be open in succession, not at once. “doScreenCapture”是一个异步函数,您应该使用
await doScreenCapture
以便连续打开页面,而不是一次打开。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.