-
Notifications
You must be signed in to change notification settings - Fork 0
/
books.js
37 lines (32 loc) · 1.06 KB
/
books.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
const fs = require("fs");
const j2cp = require("json2csv").Parser;
const axios = require("axios");
const cheerio = require("cheerio");
const mystery = "http://books.toscrape.com/catalogue/category/books/mystery_3/index.html";
const books_data = [];
async function getBooks(url) {
try {
const response = await axios.get(url);
const $ = cheerio.load(response.data);
const books = $("article");
books.each(function () {
title = $(this).find("h3 a").text();
price = $(this).find(".price_color").text();
stock = $(this).find(".availability").text().trim();
books_data.push({ title, price, stock });
});
// console.log(books_data);
const baseUrl = "http://books.toscrape.com/catalogue/category/books/mystery_3/";
if ($(".next a").length > 0) {
next = baseUrl + $(".next a").attr("href");
getBooks(next);
} else {
const parser = new j2cp();
const csv = parser.parse(books_data);
fs.writeFileSync("./books.csv", csv);
}
} catch (err) {
console.error(err);
}
}
getBooks(mystery);