-
Notifications
You must be signed in to change notification settings - Fork 102
/
Copy pathdedicatedbrand.js
56 lines (47 loc) · 1.15 KB
/
dedicatedbrand.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
46
47
48
49
50
51
52
53
54
55
56
const fetch = require('node-fetch');
const cheerio = require('cheerio');
/**
* Parse webpage e-shop
* @param {String} data - html response
* @return {Array} products
*/
const parse = data => {
const $ = cheerio.load(data);
return $('.productList-container .productList')
.map((i, element) => {
const name = $(element)
.find('.productList-title')
.text()
.trim()
.replace(/\s/g, ' ');
const price = parseInt(
$(element)
.find('.productList-price')
.text()
);
return {name, price};
})
.get();
};
/**
* Scrape all the products for a given url page
* @param {[type]} url
* @return {Array|null}
*/
module.exports.scrape = async url => {
try {
const response = await fetch(url);
if (response.ok) {
const body = await response.text();
return parse(body);
}
console.error(response);
return null;
} catch (error) {
console.error(error);
return null;
}
};
const result = await fetch('https://www.dedicatedbrand.com/en/loadfilter?category=men%2Fall-men');
const x = await result.json();
products = x.products;