Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

95
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda