-
Notifications
You must be signed in to change notification settings - Fork 0
/
crawler.js
102 lines (89 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
var request = require('request');
var cheerio = require('cheerio');
var URL = require('url-parse');
var json2csv = require('json2csv');
var fs = require('fs');
var START_URL = "https://medium.com/";
var MAX_PAGES_TO_VISIT = 100;
var pagesVisited = {};
var numPagesVisited = 0;
var pagesToVisit = [];
var url = new URL(START_URL);
var baseUrl = url.protocol + "//" + url.hostname;
var http = require('http');
var httpAgent = new http.Agent();
httpAgent.maxSockets = 5;
var cnt = 0;
var fields = ['Visiting_Page', 'Status_Code', 'Number_Of_links', 'links'];
var mainObj = [];
pagesToVisit.push(START_URL);
crawl();
function crawl() {
if (numPagesVisited >= MAX_PAGES_TO_VISIT) {
var csv = json2csv({
data: mainObj,
fields: fields
});
fs.writeFile('output.csv', csv, function(err) {
if (err) throw err;
console.log('file saved');
});
console.log("Reached max limit of number of pages to visit.");
return;
}
var nextPage = pagesToVisit.shift();
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
urlLink = url;
pagesVisited[url] = true;
numPagesVisited++;
if (url == undefined) {
return;
}
// Make the request
console.log("Visiting page " + url);
request(url, {
pool: httpAgent
}, function(error, response, body) {
console.log('----------------->' + response);
console.log("Status code: " + response.statusCode);
if (response.statusCode !== 200) {
callback();
return;
}
// Parse the document body
var $ = cheerio.load(body);
cnt++;
collectInternalLinks($, urlLink, response.statusCode);
callback();
});
}
function collectInternalLinks($, pageLink, SC) {
var relativeLinks = $("a[href^='http']");
console.log("Found " + relativeLinks.length + " relative links on page");
if (cnt < 2) {
console.log("Now Each link will be recursivly scrap to get further more links")
relativeLinks.each(function() {
pagesToVisit.push($(this).attr('href'));
});
} else {
var obj = {};
obj.Visiting_Page = pageLink;
obj.Status_Code = SC;
obj.Number_Of_links = relativeLinks.length;
var str = "";
relativeLinks.each(function() {
str = str + ($(this).attr('href')) + ',';
});
obj.links = str;
mainObj.push(obj);
}
}