-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
71 lines (60 loc) · 2.24 KB
/
index.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
var fs = require('fs');
var request = require('request');
var cheerio = require('cheerio');
var Crawler = require("crawler");
function spider(url){
request(url, function(error, response, html){
if(!error){
var $ = cheerio.load(html);
var d;
var c = new Crawler({
rateLimit: 2000, // `maxConnections` will be forced to 1
callback: function(err, res, done){
d = $('.r').children()[0].attribs.href;
console.log(d);
fs.writeFile(`data.json`,d,function (){console.log("written")});
done();
}
});
c.queue(url);
}
})
}
function go(url,name){
request(url, function(error, response, html){
if(!error){
var $ = cheerio.load(html);
var d;
var c = new Crawler({
rateLimit: 2000, // `maxConnections` will be forced to 1
callback: function(err, res, done){
//for getting href from google first link (r is th classname for every links in google)
d = $('.r').children()[0].attribs.href;
//Link in the format (/url?q=http://www) so to cut short the format to http
d = d.slice(7,d.length);
d={"name":name,"url":d};
console.log(d);
var k = JSON.parse(fs.readFileSync('data.json', 'utf8'));
k.push(d);
fs.writeFileSync(`data.json`,JSON.stringify(k),function (){console.log("written")});
// fs.appendFileSync(`data.json`,",",function (){console.log("written")})
done();
}
});
c.queue(url);
}
})
}
//MAIN CODE START FROM HERE
var obj = JSON.parse(fs.readFileSync('colleges.json', 'utf8'));
var t = []
//READ SEARCH DATA FROM FILE
fs.writeFile(`data.json`,JSON.stringify(t),function (){console.log("written")});
//CRAWL GOOGLE FOR WEBSITE LINKS OF THAT DATA
for (var i in obj)
{
console.log(obj[i])
var name = obj[i]
url = `https://www.google.co.in/search?source=hp&ei=wBGYW-v2BMjrvgTy06vgBA&q=${name}`;
go(url,name);
}