我正在通过 puppeteer 将文件下载到我的目录中。我需要将此文件上传到 s3 存储桶,因此我需要获取文件名。但问题是,这个文件名有一个时间戳,每次都会改变,所以我不能保留一个硬编码的名字。那么有没有办法每次都获得一个常量名称(即使旧文件被替换),或者如何重命名正在下载的文件?
我想过使用节点的 fs.rename() 函数,但这又需要当前文件名。
我想要一个常量文件名来硬编码,然后上传到 s3 存储桶中。
await page._client.send('Page.setDownloadBehavior', {behavior: 'allow', downloadPath: './xml'}); // This sets the directory
await page.keyboard.press('Tab');
await page.keyboard.press('Enter'); // This downloads an XML file.
Run Code Online (Sandbox Code Playgroud) 我尝试从我的数组中逐页浏览,但得到了这个:
(节点:4196)MaxListenersExceededWarning:检测到可能的 EventEmitter 内存泄漏。添加了 11 个请求侦听器。使用 Emitter.setMaxListeners() 增加限制(节点:4196) MaxListenersExceededWarning:检测到可能的 EventEmitter 内存泄漏。11 帧分离侦听器添加 d。使用 Emitter.setMaxListeners() 增加限制(节点:4196) MaxListenersExceededWarning:检测到可能的 EventEmitter 内存泄漏。添加了 11 个生命周期事件监听器。使用emitter.setMaxListeners() 增加限制(node:4196) UnhandledPromiseRejectionWarning: Error: Protocol error (Page.navigate): Target closed。at Promise (D:\Kutz\irrParse\node_modules\puppeteer\lib\Connection.js:198:56) at new Promise () at CDPSession.send (D:\Kutz\irrParse\node_modules\puppeteer\lib\Connection.js :197:12) 在导航 (D: \Kutz\irrParse\node_modules\puppeteer\lib\Page.js:520:39) 在 Page.goto (D:\Kutz\irrParse\node_modules\puppeteer\lib\Page.js:500:7) 在 uniqueLinks.forEach ( D:\Kutz\irrParse\scrape.js:26:16) at Array.forEach() at D:\Kutz\irrParse\scrape.js:25:15 at process._tickCallback (internal/process/next_tick.js: 118:7) (node:4196) UnhandledPromiseRejectionWarning:未处理的承诺拒绝。这个错误要么是因为在没有 catch 块的情况下抛出了异步函数,要么是因为拒绝了一个没有用 .catch() 处理过的承诺。(rjection id: 1) (node:4196) [DEP0018] DeprecationWarning:不推荐使用未处理的承诺拒绝。将来,未处理的承诺拒绝离子将使用非零退出代码终止 Node.js 进程。(节点:4196)未处理的PromiseRejectionWarning:
const puppeteer = require("puppeteer");
var forEach = require('async-foreach').forEach;
const url = …Run Code Online (Sandbox Code Playgroud)