【发布时间】:2022-01-02 08:54:36
【问题描述】:
我有 pdf 文件的网址。 例如:
const filesUrl= ["http://example.com/animal.pdf", "https://example.com/fruit.pdf", "http://example.com/stars.pdf"]
我想下载这些文件并使用 puppeteer 将其保存到我的服务器文件夹中。
我在做什么:
- 循环 filesUrl
- 第一个循环使用 page.goto(url)
- 使用 fs.writeFile 写入文件
- 下一个循环
但我有错误:[ERR_INVALID_CALLBACK]:回调必须是一个函数。收到未定义
如果我查看浏览器,循环不会等到页面完全加载。
这是我的代码:
const filesUrl = await page.$$eval("li.b_algo h2 a", urls => {
return urls.map(url => url.href)
})
// filesUrl = ["http://example.com/animal.pdf", "https://example.com/fruit.pdf", "http://example.com/stars.pdf"]
for (const fileUrl of filesUrl) {
try {
const filepage = await page.goto(fileUrl)
await fs.writeFile(Math.random() + ".pdf", await filepage.buffer())
} catch (error) {
console.log('errorsadasd:', error);
}
}
await browser.close()
【问题讨论】:
标签: javascript node.js web-scraping puppeteer