NodeJS 从网页创建PDF文件时,我在Puppeteer中的循环不循环

qojgxg4l  于 2023-01-20  发布在  Node.js
关注(0)|答案(1)|浏览(90)

我尝试使用Puppeteer将大约300个网页抓取为PDF,但是我的循环不起作用。目的是Puppeteer从数组中加载每个页面,生成PDF,然后在关闭之前遍历所有URL。
使用下面的代码,Puppeteer成功地抓取了第一个URL--然后停止。
代码(URL为占位符):

const puppeteer = require('puppeteer');

(async () => {
  // Create a browser instance
  const browser = await puppeteer.launch({ headless: true });

  // Create a new page
  const page = await browser.newPage();

  // Set viewport width and height
  await page.setViewport({ width: 1280, height: 720 });

  const urlArray = [
    'https://ask.metafilter.com/369890/Patio-furniture-designed-for-the-PNW',
    'https://ask.metafilter.com/369889/Its-the-police-should-I-document-my-concern',
    'https://ask.metafilter.com/369888/Training-my-over-excited-dog'
  ];

for(var i = 0; i < urlArray.length; i++) {

  const website_url = urlArray[i];

  // Open URL in current page
  await page.goto(website_url, { waitUntil: 'networkidle0' });

  // Download the PDF
  const pdf = await page.pdf({
    path: 'images/page_${i+1}.pdf',
    margin: { top: '100px', right: '50px', bottom: '100px', left: '50px' },
    printBackground: true,
  });

}
  // Close the browser instance
  await browser.close();
})();

但是,如果我试图创建一个截图,换出这个:

// Download the PDF
  const pdf = await page.pdf({
    path: 'images/page.pdf',
    margin: { top: '100px', right: '50px', bottom: '100px', left: '50px' },
    printBackground: true,
  });

为此:

// Capture screenshot
  await page.screenshot({
    path: `images/screenshot_full_${i+1}.jpg`,
    fullPage: true
  });

它循环良好,遍历数组中的每个URL。
我错过了什么?
我正在学习这些教程:https://www.bannerbear.com/blog/how-to-make-a-pdf-from-html-with-node-js-and-puppeteer/https://www.bannerbear.com/blog/how-to-take-screenshots-with-puppeteer/

epggiuax

epggiuax1#

正如@ggorlen所指出的,我在本应使用反勾号的地方使用了单引号:

path: 'images/page_${i+1}.pdf'

应为:

path: `images/page_${i+1}.pdf`

相关问题