forked from NikolaiT/scrapeulous
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoogle_scraper.js
341 lines (291 loc) · 10.6 KB
/
google_scraper.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/**
* @author Nikolai Tschacher
* @version 1.0
* @last_modified Feb 2020
* @website: incolumitas.com
*
* Searches on Google and extracts search results from the SERP.
*
* Searches exactly one keyword on N pages.
*
* Supported options:
*
* @param options.num_pages: integer, the number of pages to crawl for all keywords
* @param options.google_domain: string, the google domain to use
* @param options.google_params: object, the google query arg parameters
*
* For example:
*
* "google_params": {
* "gl": "us",
* "hl": "en",
* "start": 0,
* "num": 100
* }
*
* @param keyword: The keyword that is requested on Google
* @param this.options: Holds all configuration data and options for the Google Scraper
*/
class GoogleScraper extends BrowserWorker {
async crawl(keyword) {
let num_pages = 1;
if (this.options && this.options.num_pages) {
num_pages = this.options.num_pages;
}
let results = [];
for (let page_num = 1; page_num <= num_pages; page_num++) {
if (page_num === 1) {
await this.load_start_page();
await this.search_keyword(keyword);
}
await this.wait_for_results();
let parsed = await this.parse(keyword);
parsed.page_num = page_num;
results.push(parsed);
await this.next_page();
}
return results;
}
async parse(keyword) {
let results = await this.page.evaluate(() => {
let _text = (el, s) => {
let n = el.querySelector(s);
if (n) {
return n.innerText;
} else {
return '';
}
};
let _attr = (el, s, attr) => {
let n = el.querySelector(s);
if (n) {
return n.getAttribute(attr);
} else {
return null;
}
};
let results = {
num_results: '',
no_results: false,
effective_query: '',
right_info: {},
results: [],
top_products: [],
right_products: [],
top_ads: [],
bottom_ads: [],
places: [],
};
try {
let num_results_el = document.getElementById('extabar').querySelector('nobr').parentNode;
if (num_results_el) {
results.num_results = num_results_el.innerText;
}
} catch (err) {
let num_results_el = document.getElementById('extabar');
if (num_results_el) {
results.num_results = num_results_el.innerText;
}
}
let organic_results = document.querySelectorAll('#center_col .g');
organic_results.forEach((el, index) => {
let serp_obj = {
rank: index + 1,
link: _attr(el, '.r a', 'href'),
title: _text(el, '.r a h3'),
snippet: _text(el, 'span.st'),
visible_link: _text(el, '.r cite'),
date: _text(el, 'span.f'),
};
if (serp_obj.date) {
serp_obj.date = serp_obj.date.replace(' - ', '');
}
results.results.push(serp_obj);
});
// check if no results
results.no_results = (results.results.length === 0);
let parseAds = (container, selector) => {
document.querySelectorAll(selector).forEach((el) => {
let ad_obj = {
visible_link: _text(el, '.ads-visurl cite'),
tracking_link: _attr(el, 'a:first-child', 'href'),
link: _attr(el, 'a:nth-child(2)', 'href'),
title: _text(el, 'a h3'),
snippet: _text(el, '.ads-creative'),
links: [],
};
el.querySelectorAll('ul li a').forEach((node) => {
ad_obj.links.push({
tracking_link: node.getAttribute('data-arwt'),
link: node.getAttribute('href'),
title: node.innerText,
})
});
container.push(ad_obj);
});
};
parseAds(results.top_ads, '#tads .ads-ad');
parseAds(results.bottom_ads, '#tadsb .ads-ad');
// parse google places
document.querySelectorAll('.rllt__link').forEach((el) => {
results.places.push({
heading: _text(el, '[role="heading"] span'),
rating: _text(el, '.rllt__details div:first-child'),
contact: _text(el, '.rllt__details div:nth-child(2)'),
hours: _text(el, '.rllt__details div:nth-child(3)'),
})
});
// parse right side product information
results.right_info.review = _attr(document, '#rhs .cu-container g-review-stars span', 'aria-label');
let title_el = document.querySelector('#rhs .cu-container g-review-stars');
if (title_el) {
results.right_info.review.title = title_el.parentNode.querySelector('div:first-child').innerText;
}
let num_reviews_el = document.querySelector('#rhs .cu-container g-review-stars');
if (num_reviews_el) {
results.right_info.num_reviews = num_reviews_el.parentNode.querySelector('div:nth-of-type(2)').innerText;
}
results.right_info.vendors = [];
results.right_info.info = _text(document, '#rhs_block > div > div > div > div:nth-child(5) > div > div');
document.querySelectorAll('#rhs .cu-container .rhsvw > div > div:nth-child(4) > div > div:nth-child(3) > div').forEach((el) => {
results.right_info.vendors.push({
price: _text(el, 'span:nth-of-type(1)'),
merchant_name: _text(el, 'span:nth-child(3) a:nth-child(2)'),
merchant_ad_link: _attr(el, 'span:nth-child(3) a:first-child', 'href'),
merchant_link: _attr(el, 'span:nth-child(3) a:nth-child(2)', 'href'),
source_name: _text(el, 'span:nth-child(4) a'),
source_link: _attr(el, 'span:nth-child(4) a', 'href'),
info: _text(el, 'div span'),
shipping: _text(el, 'span:last-child > span'),
})
});
if (!results.right_info.title) {
results.right_info = {};
}
let right_side_info_el = document.getElementById('rhs');
if (right_side_info_el) {
let right_side_info_text = right_side_info_el.innerText;
if (right_side_info_text && right_side_info_text.length > 0) {
results.right_side_info_text = right_side_info_text;
}
}
// parse top main column product information
// #tvcap .pla-unit
document.querySelectorAll('#tvcap .pla-unit').forEach((el) => {
let top_product = {
tracking_link: _attr(el, '.pla-unit-title a:first-child', 'href'),
link: _attr(el, '.pla-unit-title a:nth-child(2)', 'href'),
title: _text(el, '.pla-unit-title a:nth-child(2) span'),
price: _text(el, '.pla-unit-title + div'),
shipping: _text(el, '.pla-extensions-container div:nth-of-type(1)'),
vendor_link: _attr(el,'.pla-extensions-container div > a', 'href'),
};
let merchant_node = el.querySelector('.pla-unit-title');
if (merchant_node) {
let node = merchant_node.parentNode.querySelector('div > span');
if (node) {
top_product.merchant_name = node.innerText;
}
}
results.top_products.push(top_product);
});
// parse top right product information
// #tvcap .pla-unit
document.querySelectorAll('#rhs_block .pla-unit').forEach((el) => {
let right_product = {
tracking_link: _attr(el, '.pla-unit-title a:first-child', 'href'),
link: _attr(el, '.pla-unit-title a:nth-child(2)', 'href'),
title: _text(el, '.pla-unit-title a:nth-child(2) span:first-child'),
price: _text(el,'.pla-unit-title + div'),
shipping: _text(el,'.pla-extensions-container > div'),
vendor_link: _text(el,'.pla-extensions-container div > a'),
vendor_name: _text(el,'.pla-extensions-container div > a > div'),
};
let merchant_node = el.querySelector('.pla-unit-title');
if (merchant_node) {
let node = merchant_node.parentNode.querySelector('div > span:first-child');
if (node) {
right_product.merchant_name = node.innerText;
}
}
results.right_products.push(right_product);
});
let effective_query_el = document.getElementById('fprsl');
if (effective_query_el) {
results.effective_query = effective_query_el.innerText;
if (!results.effective_query) {
let effective_query_el2 = document.querySelector('#fprs a');
if (effective_query_el2) {
results.effective_query = document.querySelector('#fprs a').innerText;
}
}
}
return results;
});
// clean some results
results.top_products = this.clean_results(results.top_products, ['title', 'link']);
results.right_products = this.clean_results(results.right_products, ['title', 'link']);
results.results = this.clean_results(results.results, ['title', 'link' , 'snippet']);
results.time = (new Date()).toUTCString();
return results;
}
async load_start_page() {
let startUrl = 'https://www.google.com';
if (this.options && this.options.google_domain) {
startUrl = `https://www.${this.options.google_domain}/search?`;
} else {
startUrl = `https://www.google.com/search?`;
}
if (this.options && this.options.google_params) {
for (let key in this.options.google_params) {
startUrl += `${key}=${this.options.google_params[key]}&`
}
}
this.logger.info('Using startUrl: ' + startUrl);
await this.page.goto(startUrl);
await this.page.waitForSelector('input[name="q"]');
}
async set_input_value(selector, value) {
await this.page.waitFor(selector);
await this.page.evaluate((value, selector) => {
return document.querySelector(selector).value = value;
}, value, selector);
}
/*
Throw away all elements that do not have data in the
specified attributes. Most be of value string.
*/
clean_results(results, attributes) {
let cleaned = [];
for (let res of results) {
let goodboy = true;
for (let attr of attributes) {
if (!res[attr] || !res[attr].trim()) {
goodboy = false;
break;
}
}
if (goodboy) {
cleaned.push(res);
}
}
return cleaned;
}
async search_keyword(keyword) {
const input = await this.page.$('input[name="q"]');
await this.set_input_value(`input[name="q"]`, keyword);
await this.sleep(50);
await input.focus();
await this.page.keyboard.press("Enter");
}
async next_page() {
let next_page_link = await this.page.$('#pnnext');
if (next_page_link) {
await next_page_link.click();
}
}
async wait_for_results() {
await this.page.waitForSelector('#center_col .g');
await this.page.waitFor(100);
}
}