javascript - 如何使用 Puppeteer 捕获一系列 URL 的屏幕截图?
问题描述
我在异步函数内部设置了一个循环来遍历 URL 列表。数据来自转换为 .json 的 .xls 文件。我的目标是捕获数组中每个 URL 的屏幕截图,但我不断收到 UnhandledPromiseRejectionWarning。任何想法我如何实现这一目标?任何帮助表示赞赏!
编码:
const puppeteer = require("puppeteer");
const excel = require("./excel");
const data = excel.data;
async function run(arr) {
for (let i = 0; i < data.length; i++) {
const url = data[i]["Seller"];
const sku = data[i]["Seller Name"];
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.setViewport({
width: 1000,
height: 840,
deviceScaleFactor: 1
});
await page.goto(url, { waitUntil: "load" });
await page.screenshot({ path: `screenshots/${sku}.jpg` });
await browser.close();
}
}
run(data)
try ... catch 完全错误
(node:24804) UnhandledPromiseRejectionWarning: ReferenceError: err is not defined
at run (C:\Users\ray\OneDrive\Desktop\nodeScrappingProject\app\index.js:24:19)
(node:24804) UnhandledPromiseRejectionWarning: Unhandled promise rejection. 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(). (rejection id: 1)
(node:24804) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
没有尝试的完整错误消息.. catch
(node:34456) UnhandledPromiseRejectionWarning: Error: ENOENT: no such file or directory, open 'C:\Users\ray\OneDrive\Desktop\nodeScrappingProject\app\screenshots\fileName.jpg'
-- ASYNC --
at Page.<anonymous> (C:\Users\ray\OneDrive\Desktop\nodeScrappingProject\node_modules\puppeteer\lib\helper.js:111:15)
at run (C:\Users\ray\OneDrive\Desktop\nodeScrappingProject\app\index.js:20:16)
at processTicksAndRejections (internal/process/task_queues.js:85:5)
(node:34456) UnhandledPromiseRejectionWarning: Unhandled promise rejection. 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(). (rejection id: 1)
(node:34456) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
解决方案
Your code seems correct to me. Did you try looking at what is coming out in data Seller and SellerName? I am guessing that you have issues in data. As you are not asserting anything you need not close the browser each time. Checkout the following snippet.
const puppeteer = require('puppeteer');
(async () => {
const data = [{"sellerUrl" : "https://www.amazon.com/",
"sellerName" : "amazon"
},
{"sellerUrl" : "https://www.ebay.com/",
"sellerName" : "ebay"
},
];
const browser = await puppeteer.launch();
const page = await browser.newPage();
for(const x of data){
await page.goto(x.sellerUrl, { waitUntill: 'networkidle2'});
await page.screenshot({path: `${x.sellerName}.jpg`});
}
await browser.close();
})();
推荐阅读
- api - 方法的实现不可用 错误
- apache - .htaccess 自定义文件夹中的 php url
- typescript - 返回接口的实例而不声明变量而不进行强制转换
- flutter - 从控制器颤动中分配变量值
- python - 从文本文件中提取变量的值
- php - 如何在laravel中搜索具有数组属性的对象数组
- vb.net - 如何在 VB.net 中动态编译代码?动态检索成员变量名
- python - 使用黄砖库的预测误差图问题(回归)
- java - 休眠。如何将条目添加到具有来自 Hibernate 中 2 个不同表的 2 个外键的表中?
- php - 尝试将 php-cgi.exe 文件链接到 PHP 管理器扩展时出现 IIS 错误:“由于对象的当前状态,操作无效”