如何使用 Puppeteer 下载 pdf

Nic*_*ngs 6 chromium node.js puppeteer

我正在尝试使用 Puppeteer 进行一些网页抓取,但我不确定如何实际下载我找到的文档。具体来说,我想从这样的页面下载pdf 。我的代码中尝试下载 pdf 的部分当前如下所示(注释行是下载尝试不起作用):

const newPagePromise = new Promise(x =>
  browser.once("targetcreated", target => x(target.page()))
);
await page.click(
  "#gvDocketResult_ctl0" + rows.length + "_hlDocumentRedacted"
);
await page.waitFor(3000);
const newPage = await newPagePromise;
// need to figure out how to download
await newPage._client.send("Page.setDownloadBehavior", {
  behavior: "allow",
  downloadPath: "/Users/me/Desktop",
});
// await newPage.pdf({path: 'hn.pdf', format: 'letter'});
// await newPage.click('#download');
// await newPage.click('#icon');
Run Code Online (Sandbox Code Playgroud)

从我到目前为止发现的情况来看,如果我可以获得src = ''网页部分中显示的链接(下图),那么我也许可以使用 page.goto(link) 来下载 pdf?无论如何,我不知道如何在 puppeteer 中访问该链接,因此如果有人对此有建议,我也将不胜感激。在此输入图像描述

小智 -1

const your_url = " put your url here "

const page2 = await browser.newPage()

const dir='C:/the directory you want to place the file';

const client = await page.target().createCDPSession()
await client.send('Page.setDownloadBehavior', {behavior: 'allow', downloadPath: path.resolve(dir)});

async function downloadFile(page, link) {                                                                                                                    
     
    return page.evaluate((link) =>
   {  
      location.href = link;
   
    },link);                                                                                                                    
}

downloadFile(page2,your_url)
Run Code Online (Sandbox Code Playgroud)