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

215
Views
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 answers
Answer question

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 Report

0

use append flag:

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