Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

92
Vistas
Node JS Insert text from file to name result file from Puppeteer scrap

I am hacking together a node script in Puppeteer where I scrap a webpage and safe the content as a text file where I name the file after the date and want to add text from a text file at the end of the name.

const puppeteer = require('puppeteer');
const fs = require('fs');

let utc_date_string = new Date().toLocaleString("en-US", { timeZone: "UTC" });
let date_utc = new Date(utc_date_string);
let year = date_utc.getFullYear();
let month = ("0" + (date_utc.getMonth() + 1)).slice(-2);
let date = ("0" + date_utc.getDate()).slice(-2);
let hours = ("0" + date_utc.getHours()).slice(-2);
let minutes = ("0" + date_utc.getMinutes()).slice(-2);
let time_hh_mm = hours +  + minutes;
let data = fs.readFileSync('insert.txt', 'utf8');

(async () => {
  const browser = await puppeteer.launch(
{
    headless: true,
    args: ['--single-process', '--no-zygote', '--no-sandbox']
  })
  const page = await browser.newPage()
await page.goto('https://example.com/xxx.html')
  const myproblem = await page.$eval('table', table => table.innerText)

fs.writeFile('/var/www/mydomain.com/xxx/'+year+'-'+month+'-'+date+'-'+time_hh_mm+'-'+data+'.txt', myproblem)
  await browser.close()
})()

This works so far but the problem is that the file name comes out like this

'2022-02-13-1453-test_text_insert'$'\n''.txt'

and not like this

2022-02-13-1453-test_text_insert.txt

If I use only the date, the file name comes out as expected. I don't know from where the extra characters come from (they are not in the text file) or if there is something wrong with the code.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

String error. You should check this line:

let time_hh_mm = hours +  + minutes;

Maybe it should be like this: (?)

let time_hh_mm = hours + minutes;

And you tried to read the file which contain the breakline character \n, you can try to trim it.

fs.writeFile('/var/www/mydomain.com/xxx/'+year+'-'+month+'-'+date+'-'+time_hh_mm+'-'+data.trim()+'.txt', myproblem)
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda