I am attempting to write ${car}, ${link}, ${price} to a csv file. My current code does that but it runs my functions more then once and I end up with ${car}, ${link} and ${price} 5 separate times in my csv file I only want it once. Any help would be much appreciated! Thank you in advance!
const request = require('request'); const cheerio = require('cheerio'); const fs = require('fs'); const writeStream = fs.createWriteStream('post.csv'); //Write Headers writeStream.write(`Title,Link,Price n`) request('https://bringatrailer.com/bmw/e46/?q=e46', (error, response, html) => { if(!error && response.statusCode == 200) { const $ = cheerio.load(html); //Grabs the title and link of the featured e46 listings off of bring a trailer $('.featured-listing-title-link').each((a, title) => { const car = $(title).text(); const link = $(title).attr('href'); //Grabs the prices of the featured e46 listings off of bring a trailer $('.featured-listing-meta-value').each((i, value) => { const price = $(value).text().replace(/,/,""); writeStream.write(`${car}, ${link}, ${price} n`); }); }); //Write to CSV console.log('Scraping Complete...') } });
just for reference this is what I would like my csv file to look like
Advertisement
Answer
According to the DOM structure of this specific website, this is what worked on my end.
const request = require("request"); const cheerio = require("cheerio"); const fs = require("fs"); const writeStream = fs.createWriteStream("post.csv"); writeStream.write(`Title,Link,Price n`); request("https://bringatrailer.com/bmw/e46/?q=e46", (error, response, html) => { if (!error && response.statusCode == 200) { const $ = cheerio.load(html); var counter = 0; $(".featured-listing-title-link").each((a, title) => { const car = $(title).text(); const link = $(title).attr("href"); const pricetag = cheerio.load($(".featured-listing-meta-value")[counter]); const price = pricetag.text(); //Grabs the prices of the featured e46 listings off of bring a trailer writeStream.write(`${car}, ${link}, ${price} n`); counter += 2; }); //Write to CSV console.log("Scraping Complete..."); } });