-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrapper.js
59 lines (51 loc) · 1.61 KB
/
scrapper.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
57
58
59
const axios = require("axios");
const cheerio = require("cheerio");
/**
* @property {string} url - The URL to fetch html from.
* @property {string} htmlResult - The fetched html result.
* @property {string} selecor - The selecor that will loop for each one.
* @property {Array} data - The data final data.
*/
class Scraper {
/**
* @param {string} url - The url to scrape.
* @param {string} selecor - The selecor for the elements you wanna loop through.
* */
constructor(url, selecor = "") {
this.url = url;
this.htmlResult = "";
this.selecor = selecor;
this.data = [];
}
/**
* @return {Promise} - fetch the html from the url and assigne it to the htmlResult variable.
* */
async fetchUrl() {
this.htmlResult = await axios.get(this.url);
}
/**
* @retrun {Scraper} - use cheerio to load the htmlResult and get the needed data and add it to the data array.
* */
async getData() {
await this.fetchUrl();
const $ = cheerio.load(this.htmlResult.data);
$("#preview")
.find(this.selecor)
.each((index, element) => {
const deal = {
link: this.url + $(element).find(".more").attr("href"),
time: $(element).find(".bundle-time").text(),
title: $(element).find(".bundle-title a").text(),
shopTitle: $(element).find(".bundle-title .shopTitle").text(),
};
this.data.push(deal);
});
return this;
}
}
// new Scraper("https://isthereanydeal.com/", ".cntBoxContent .bundle-preview")
// .getData()
// .then(function (data) {
// console.log(data.data);
// });
module.exports = Scraper;