簡體   English   中英

如何使用 Puppeteer 捕獲一組 URL 的屏幕截圖?

[英]How would I capture screenshots of an array of URLs using Puppeteer?

我在異步函數內設置了一個循環來遍歷 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.

你的代碼對我來說似乎是正確的。 您是否嘗試查看數據 Seller 和 SellerName 中出現的內容? 我猜你在數據方面有問題。 由於您沒有斷言任何內容,因此無需每次都關閉瀏覽器。 查看以下代碼段。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM