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

216
Visualizações
Scraping a web page for .pdf link's and writing all of the matching links to a text file in nodeJS

I am an aspiring developer. As one of my projects, I am learning how to do web scraping. The goal is here to scrape a given webpage for any links that are PDFs and saving those links to a text file in NodeJS. With the given code, I am successfully console logging all of the matching links, but I am only getting one file written to my text file. Can someone steer me into the right direction?

const puppeteer = require("puppeteer");
const fs = require("fs/promises");

let myNewURL =
  "https://www.renault.co.il/cars/Zoe/index.html?fbclid=IwAR1RtxbC_U2fImp9_KXJuQ869h5Wv77fyZVj8uBOU86rU90wb2L_NfrNppc";

async function scrapeSite(url) {
  console.log("firing");
  const browser = await puppeteer.launch({ headless: true });
  const page = await browser.newPage();
  await page.goto(url);

  //this gives back an actual array, not a node list
  const linkCollection = await page.$$eval("a", (links) => {
    return links.map((link) => {
      return link.href;
    });
  });

  for (const link of linkCollection) {
    if (link.includes(".pdf")) {
      console.log(link);
      await fs.writeFile("pdfLinks.txt", link);
    }
  }

  await browser.close();
}

scrapeSite(myNewURL);

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

fs.writeFile overwrites the original file with each call. Try fs.appendFile instead. I've also added a newline (\n) at the end so the links are on individual lines:

for (const link of linkCollection) {
  if (link.includes(".pdf")) {
    console.log(link);
    await fs.appendFile("pdfLinks.txt", link + '\n');
  }
}

Alternatively, you could collect the links into an array first, then write them all together:

const pdfLinks = [];

for (const link of linkCollection) {
  if (link.includes(".pdf")) {
    console.log(link);
    pdfLinks.push(link);
  }
}

const output = pdfLinks.join('\n')
await fs.writeFile("pdfLinks.txt", output);
about 4 years ago · Juan Pablo Isaza Relatório

0

use append flag:

await fs.writeFile("pdfLinks.txt", link+'\n',{flag:'a'});
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