-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
47 lines (41 loc) · 1.47 KB
/
main.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
// Scrapping data from this URL
const url = "https://www.espncricinfo.com/series/ipl-2020-21-1210595";
// Creating File via File System Module (predefined in node)
const fs = require("fs");
// Setting generic path so that main.js could be called at some other location as well (path - predefined module in js)
const path = require("path");
// DATA REQUESTED: Venue date opponent result runs balls fours sixes sr
const request = require("request");
// Cheerio is used to parse the extracted html data
const cheerio = require("cheerio");
const AllMatcgObj = require("./Allmatch");
// Create IPL folder (that stores the extracted data) in the same curr directory
const iplPath = path.join(__dirname, "ipl");
dirCreater(iplPath);
request(url, cb);
// Async func
function cb(err, response, html) { // cb = callback
if (err) {
console.log(err);
} else {
// console.log(html);
extractLink(html);
}
}
// Extract data via css selector
function extractLink(html) {
let $ = cheerio.load(html);
let anchorElem = $("a[data-hover='View All Results']");
//
let link = anchorElem.attr("href");
// console.log(link);
let fullLink = "https://www.espncricinfo.com" + link;
console.log(fullLink);
AllMatcgObj.gAlmatches(fullLink);
}
// Create directory if not there else only update it
function dirCreater(filePath) {
if (fs.existsSync(filePath) == false) {
fs.mkdirSync(filePath);
}
}