我在异步函数内部设置了一个循环,以遍历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)
尝试...捕获完全错误
(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.
答案 0 :(得分:0)
您的代码对我来说似乎是正确的。您是否尝试查看数据卖方和卖方名称中出现了什么?我猜想您的数据有问题。由于您未声明任何内容,因此无需每次都关闭浏览器。检出以下代码段。
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();
})();