【问题标题】:How would I capture screenshots of an array of URLs using Puppeteer?如何使用 Puppeteer 捕获一系列 URL 的屏幕截图?
【发布时间】:2019-12-08 20:14:40
【问题描述】:

我在异步函数内部设置了一个循环来遍历 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.

没有 try .. 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.

【问题讨论】:

    标签: javascript arrays node.js json puppeteer


    【解决方案1】:

    您的代码对我来说似乎是正确的。您是否尝试查看数据卖方和卖方名称中出现的内容?我猜你的数据有问题。由于您没有断言任何内容,因此您无需每次都关闭浏览器。查看以下 sn-p。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-04-06
      • 1970-01-01
      • 2014-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多