forked from thibauts/node-google-search-scraper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
169 lines (133 loc) · 4.38 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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
var request = require('request');
var cheerio = require('cheerio');
var url = require('url');
var session = request.defaults({ jar : true });
session.get({
uri: 'https://www.google.com/ncr'
},
function(err, res) {}
) // create request and remember that there should not be country specific redirect
function search(options, callback) {
var host = options.host || 'www.google.com';
var solver = options.solver;
var params = options.params || {};
var results = {
foundPages: [],
};
var currentResults;
params.hl = params.hl || options.lang || 'en';
if(options.age) params.tbs = 'qdr:' + options.age;
if(options.query) params.q = options.query;
params.start = 0;
getPage(params, function onPage(err, body) {
if(err) {
if(err.code !== 'ECAPTCHA' || !solver) return callback(err);
solveCaptcha(err.location, function(err, page) {
if(err) return callback(err);
onPage(null, page);
});
return;
}
if (
body.indexOf("it's really you sending the requests, and not a robot") != -1 ||
body.indexOf("but your computer or network may be sending automated queries") != -1
) {
// we've got google captcha now
return solver.solve();
}
if(options.extractResults && typeof options.extractResults === "function") {
currentResults = options.extractResults(body);
} else {
currentResults = extractResults(body);
}
if (options.oneCallbackForAllResults) {
callback(null, currentResults);
} else {
currentResults.foundPages.forEach(function(result) {
callback(null, result);
});
}
if(currentResults.foundPages.length === 0) {
return;
}
results.foundPages = results.foundPages.concat(currentResults);
if(!options.limit || results.length < options.limit) {
params.start = results.length;
getPage(params, onPage);
}
});
function getPage(params, callback) {
session.get({
uri: 'https://' + host + '/search',
qs: params,
headers: {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.75.14 (KHTML, like Gecko) Version/7.0.3 Safari/7046A194A'
},
followRedirect: false
},
function(err, res) {
if(err) return callback(err);
if(res.statusCode === 302) {
var err = new Error('Captcha');
err.code = 'ECAPTCHA';
err.location = res.headers.location;
this.abort();
return callback(err);
}
callback(null, res.body);
}
);
}
function extractResults(body) {
var results = {
foundPages: [],
};
var $ = cheerio.load(body);
$('.g h3 a').each(function(i, elem) {
var parsed = url.parse(elem.attribs.href, true);
if (parsed.pathname === '/url') {
results.foundPages.push(parsed.query.q);
}
});
return results;
}
function solveCaptcha(captchaUrl, callback) {
var tmp = url.parse(captchaUrl);
var baseUrl = url.format({
protocol: tmp.protocol,
hostname: tmp.host,
});
// Fetch captcha page
session.get(captchaUrl, function(err, res) {
if(err) return callback(err);
var $ = cheerio.load(res.body);
var captchaId = $('input[name=id]').attr('value');
var continueUrl = $('input[name=continue]').attr('value');
var formAction = $('form').attr('action');
var imgSrc = $('img').attr('src');
// Fetch captcha image
session.get({uri: baseUrl + imgSrc, encoding: null}, function(err, res) {
if(err) return callback(err);
// Send to solver
solver.solve(res.body, function(err, id, solution) {
if(err) return callback(err);
// Try solution
session.get({
uri: baseUrl + '/sorry/' + formAction,
qs: {
id: captchaId,
captcha: solution,
continue: continueUrl
}
},
function(err, res) {
if(res.statusCode !== 200) return callback(new Error('Captcha decoding failed'));
callback(null, res.body);
}
);
});
});
});
}
}
module.exports.search = search;