-
Notifications
You must be signed in to change notification settings - Fork 0
/
crawler.js
109 lines (83 loc) · 2.67 KB
/
crawler.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
var request = require('request');
var cheerio = require('cheerio');
var URL = require('url-parse');
var START_URL = "http://www.arstechnica.com";
var SEARCH_WORD = "stemming";
var MAX_PAGES_TO_VISIT = 10;
var pagesVisited = {};
var numPagesVisited = 0;
var pagesToVisit = [];
var url = new URL(START_URL);
var baseUrl = url.protocol + "//" + url.hostname;
pagesToVisit.push(START_URL);
crawl();
function crawl() {
if(numPagesVisited >= MAX_PAGES_TO_VISIT) {
console.log("Reached max limit of number of pages to visit.");
return;
}
var nextPage = pagesToVisit.pop();
if (nextPage in pagesVisited) {
// We've already visited this page, so repeat the crawl
crawl();
} else {
// New page we haven't visited
visitPage(nextPage, crawl);
}
}
function visitPage(url, callback) {
// Add page to our set
pagesVisited[url] = true;
numPagesVisited++;
// Make the request
console.log("Visiting page " + url);
request(url, function(error, response, body) {
// Check status code (200 is HTTP OK)
console.log("Status code: " + response.statusCode);
if(response.statusCode !== 200) {
callback();
return;
}
// Parse the document body
var $ = cheerio.load(body);
var isWordFound = searchForWord($, SEARCH_WORD);
if(isWordFound) {
console.log('Word ' + SEARCH_WORD + ' found at page ' + url);
} else {
collectInternalLinks($);
// In this short program, our callback is just calling crawl()
callback();
}
});
}
//this code will parse the page and search for a given word
function searchForWord($, word) {
var bodyText = $('html > body').text();
if(bodyText.toLowerCase().indexOf(word.toLowerCase()) !== -1) {
return true;
}
return false;
}
//gathers all relative links and add to pagesToVisit array
function collectInternalLinks($) {
var relativeLinks = $("a[href^='/']");
console.log("Found " + relativeLinks.length + " relative links on page");
relativeLinks.each(function() {
pagesToVisit.push(baseUrl + $(this).attr('href'));
});
}
//this code will gather all of the relative and absolute hyperlinks on a given page
// function collectInternalLinks($) {
// var allRelativeLinks = [];
// var allAbsoluteLinks = [];
// var relativeLinks = $("a[href^='/']");
// relativeLinks.each(function() {
// allRelativeLinks.push($(this).attr('href'));
// });
// var absoluteLinks = $("a[href^='http']");
// absoluteLinks.each(function() {
// allAbsoluteLinks.push($(this).attr('href'));
// });
// console.log("Found: "+ allRelativeLinks.length + " relative links.");
// console.log("Found: "+ allAbsoluteLinks.length + " absolute links.");
// }