Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

254
Views
Titiritero page.goto (url) dentro del bucle sin esperar hasta que se cargue

Tengo urls de archivo pdf. ex:

 const filesUrl= ["http://example.com/animal.pdf", "https://example.com/fruit.pdf", "http://example.com/stars.pdf"]

Quiero descargar esos archivos y guardarlos en la carpeta de mi servidor usando titiritero.

Lo que estoy haciendo es:

  1. URL de archivos en bucle
  2. primer bucle estoy usando page.goto (url)
  3. escribir archivo usando fs.writeFile
  4. siguiente bucle

Pero tengo un error: [ERR_INVALID_CALLBACK]: la devolución de llamada debe ser una función. Recibido indefinido

Si miro el navegador, el bucle no espera hasta que la página esté completamente cargada.

este es mi código:

 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()
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

El error no está relacionado con el puppeteer , sino con fs.writeFile , que requiere una devolución de llamada como argumento, que falta, es decir, no está undefined , de ahí el error.

Intenta agregar la devolución de llamada:

 await fs.writeFile(Math.random() + ".pdf", await filepage.buffer(), () => { console.log('done writing file'); });
about 4 years ago · Juan Pablo Isaza Report

0

Intente usar writeFile, pero envuelto en una promesa:

 import fs from 'fs'; const fsPromises = fs.promises; const writeFilePromise = fsPromises.writeFile; await writeFilePromise(Math.random() + ".pdf", await filepage.buffer());
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!