首页 > 解决方案 > 如何使用 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.

标签: javascriptarraysnode.jsjsonpuppeteer

解决方案


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();
})();

推荐阅读