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!
JavaScript
x
36
36
1
const request = require('request');
2
const cheerio = require('cheerio');
3
const fs = require('fs');
4
const writeStream = fs.createWriteStream('post.csv');
5
6
//Write Headers
7
8
writeStream.write(`Title,Link,Price n`)
9
10
request('https://bringatrailer.com/bmw/e46/?q=e46', (error, response, html) => {
11
if(!error && response.statusCode == 200) {
12
const $ = cheerio.load(html);
13
14
15
//Grabs the title and link of the featured e46 listings off of bring a trailer
16
$('.featured-listing-title-link').each((a, title) => {
17
const car = $(title).text();
18
const link = $(title).attr('href');
19
20
21
22
//Grabs the prices of the featured e46 listings off of bring a trailer
23
$('.featured-listing-meta-value').each((i, value) => {
24
const price = $(value).text().replace(/,/,"");
25
26
writeStream.write(`${car}, ${link}, ${price} n`);
27
});
28
});
29
30
31
32
//Write to CSV
33
console.log('Scraping Complete...')
34
}
35
});
36
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.
JavaScript
1
28
28
1
const request = require("request");
2
const cheerio = require("cheerio");
3
const fs = require("fs");
4
const writeStream = fs.createWriteStream("post.csv");
5
6
7
writeStream.write(`Title,Link,Price n`);
8
9
request("https://bringatrailer.com/bmw/e46/?q=e46", (error, response, html) => {
10
if (!error && response.statusCode == 200) {
11
const $ = cheerio.load(html);
12
13
var counter = 0;
14
$(".featured-listing-title-link").each((a, title) => {
15
const car = $(title).text();
16
const link = $(title).attr("href");
17
const pricetag = cheerio.load($(".featured-listing-meta-value")[counter]);
18
const price = pricetag.text();
19
//Grabs the prices of the featured e46 listings off of bring a trailer
20
writeStream.write(`${car}, ${link}, ${price} n`);
21
counter += 2;
22
});
23
24
//Write to CSV
25
console.log("Scraping Complete...");
26
}
27
});
28