-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinksExtractor.js
52 lines (47 loc) · 1.58 KB
/
LinksExtractor.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
const axios = require('axios');
const fs = require('fs');
const links = require('./data/LaptopLinks.json');
const jsdom = require('jsdom');
const { JSDOM } = jsdom;
//Scrapes links to specifications pages
let totalCount
let totalpgs=3;
let pg = 1;
let spUrl;
async function LinksExtractor(){
while(pg != totalpgs){
spUrl= "https://www.smartprix.com/laptops/exclude_global-exclude_out_of_stock-exclude_upcoming-stock?uq=1&page="+String(pg);
console.log(spUrl);
await axios.get(spUrl)
.then(function (response) {
// handle success
let frag = JSDOM.fragment(String(response.data));
console.log("pages : "+pg);
if(pg == 1){
totalCount = parseInt(frag.querySelector(".section-header strong").getAttribute("data-count"));
console.log(totalCount);
totalpgs = Math.ceil(totalCount/20)+1;
}
//loop save an item and delete it to get next element as jsdom give only first element
for(let i=0;; i++){
if(frag.querySelector(".info h2")){
links[`${frag.querySelector(".info h2").textContent}`] = "https://www.smartprix.com" + frag.querySelector(".info h2 a").getAttribute("href");
frag.querySelector(".info h2").remove();
}else{
break;
}
}
fs.writeFileSync('data/LaptopLinks.json',JSON.stringify(links));
pg = pg + 1;
})
.catch(function (error) {
// handle error
console.log(error);
})
.then(function () {
// always executed
});
}
}
LinksExtractor();
module.exports = LinksExtractor;