-
Notifications
You must be signed in to change notification settings - Fork 0
/
categoriesProducer.js
45 lines (37 loc) · 1.62 KB
/
categoriesProducer.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
38
39
40
41
42
43
44
45
const puppeteer = require('puppeteer');
const stringify = require('csv-stringify');
const fs = require('fs');
(async () => {
var browser = await puppeteer.launch();
browser.newPage().then((page) => {
page.goto('https://www.jumia.com.gh/index/allcategories/',{timeout:15000}).then(() => {
console.log('Successfully navigated to category link: https://www.jumia.com.gh/index/allcategories/');
// Scrape Category Page
page.$$eval('a.-gy5',categoryLinkElements => {
let categories = [];
for(let i = 0; i < categoryLinkElements.length; i++) {
let category = {};
category.name = categoryLinkElements[i].innerText;
category.link = categoryLinkElements[i].href + '?shipped_from=country_local';
categories.push(category);
}
return categories;
}).then(categories => {
// Create & Save Categories in categories.csv
categories.forEach((category,index) => {
stringify([category],{
header: (index == 0),
quoted:true,
columns:[{key:'name', header:'name'},{key:'link', header:'link'},],
},(error, csv_string) => {
if (error) throw error;
fs.appendFile('categories.csv', csv_string, (err) => {
if (err) throw err;
console.log(`Operation Complete. Successfully Added Category: '${category.name}' to categories.csv`);
});
});
});
});
});
});
})();