-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustom.js
69 lines (64 loc) · 2.04 KB
/
custom.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
60
61
62
63
64
65
66
67
68
69
const axios = require("axios");
const cheerio = require("cheerio");
const isRelativeUrl = require("is-relative-url");
async function getAllLinks(url) {
try {
let result = await axios.get(url);
$ = cheerio.load(result.data);
links = [];
$("a").each((i, link) => {
links.push(link);
});
return links;
} catch (err) {
console.log(err);
return err;
}
}
async function crawlPage(url) {
let links = await getAllLinks(url);
for (let link of links) {
try {
let resp = {};
if (isRelativeUrl($(link).attr("href"))) {
resp = await axios.get(url + $(link).attr("href"));
if (resp.status !== 200) {
console.log(
"Broken Link: " +
url + $(link).attr("href") +
" returned status: " +
resp.status
);
} else {
console.log(
"Valid Link: " +
url + $(link).attr("href") +
" returned status: " +
resp.status
);
}
} else {
resp = await axios.get($(link).attr("href"));
if (resp.status !== 200) {
console.log(
"Broken Link: " +
$(link).attr("href") +
" returned status: " +
resp.status
);
} else {
console.log(
"Valid Link: " +
$(link).attr("href") +
" returned status: " +
resp.status
);
}
}
} catch (err) {
console.log("Not a valid URL: " + $(link).attr("href"));
}
}
}
crawlPage("https://www.chase.com/");
// crawlPage("https://tutorialedge.net");