-
Notifications
You must be signed in to change notification settings - Fork 1
/
get_mreids.js
308 lines (221 loc) · 9.12 KB
/
get_mreids.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
const puppeteer = require('puppeteer');
const argv = require('minimist')(process.argv.slice(2));
const fs = require('fs');
// ========================== Declare Variables ==========================
const keyword = argv.kw;
const depth = argv.depth;
const output = argv.output;
let time = new Date().toLocaleString("de-DE",{year: 'numeric', month: '2-digit', day: '2-digit'});
let timestamp = new Date().getTime();
// const entity_link_selector = `//a[@class='EbH0bb']/@href`;
const entity_link_selector = `#rhs_block > div.g.rhsvw.kno-kp.mnr-c.g-blk > div.kp-blk.knowledge-panel.Wnoohf.OJXvsb > div > div.ifM9O > div:nth-child(2) > div.NFQFxe.yp1CPe.mod > div.AAXrR.lfNb6b > div.hKuTtf > a`;
let dataToWrite;
// ========================== Declare Functions ==========================
async function asyncForEach(array, callback) {
for (let index = 0; index < array.length; index++) {
await callback(array[index], index, array);
}
}
const waitFor = (ms) => new Promise(r => setTimeout(r, ms));
async function go_and_extract_entity_1(browser,page,url,keyword){
await page.goto(url, {waitUntil: 'load'});
// await page.waitFor(1000);
const data_from_page = await page.evaluate(keyword => {
let entities_tmp = new Object();
let name_of_entity;
let mreid;
let dataAttribute;
try {
try {
dataAttribute_tmp = document.getElementsByClassName("DssFqf OIQuDd")[0];
dataAttribute_tmp = dataAttribute_tmp.getAttribute('href');
dataAttribute_tmp = dataAttribute_tmp.match("%3D%2F(.*?)%26hl");
dataAttribute = dataAttribute_tmp[1];
} catch (e) { dataAttribute = "No MREID found!";}
if (dataAttribute == "_"){
try {
dataAttribute_tmp = document.getElementsByClassName("bia")[0].getAttribute('href');
dataAttribute = dataAttribute_tmp.split('%252C')[2].split('&vet')[0];
} catch (e) {
dataAttribute = "No MREID found!";
}
}
name_of_entity = document.getElementsByClassName('kno-ecr-pt kno-fb-ctx PZPZlf gsmt')[0].innerText;
if (name_of_entity.includes('\n') == true) {
name_of_entity = name_of_entity.replace('\n', ' ');
}
if (dataAttribute)
mreid = dataAttribute.replace(/%2F/g, '/');
} catch (e) {
name_of_entity = keyword;
mreid = "0";
}
return {
entity: mreid,
entity_name: name_of_entity
};
},keyword);
await page.waitFor(1000);
const entity = data_from_page.entity;
const entity_name = data_from_page.entity_name;
return {
entity: entity,
entity_name: entity_name
};
};
async function go_and_extract_entity(browser,url,keyword){
const page = await browser.newPage();
await page.setUserAgent('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36');
await page.setViewport({
width: 1920,
height: 1080
});
await page.goto(url, {waitUntil: 'load'});
// await page.waitFor(1000);
const data_from_page = await page.evaluate(keyword => {
let entities_tmp = new Object();
let name_of_entity;
let mreid;
let dataAttribute;
try {
try {
dataAttribute_tmp = document.getElementsByClassName("DssFqf OIQuDd")[0];
dataAttribute_tmp = dataAttribute_tmp.getAttribute('href');
dataAttribute_tmp = dataAttribute_tmp.match("%3D%2F(.*?)%26hl");
dataAttribute = dataAttribute_tmp[1];
} catch (e) { dataAttribute = "No MREID found!";}
if (dataAttribute == "_"){
try {
dataAttribute_tmp = document.getElementsByClassName("bia")[0].getAttribute('href');
dataAttribute = dataAttribute_tmp.split('%252C')[2].split('&vet')[0];
} catch (e) {
dataAttribute = "No MREID found!";
}
}
name_of_entity = document.getElementsByClassName('kno-ecr-pt kno-fb-ctx PZPZlf gsmt')[0].innerText;
if (name_of_entity.includes('\n') == true) {
name_of_entity = name_of_entity.replace('\n', ' ');
}
if (dataAttribute)
mreid = dataAttribute.replace(/%2F/g, '/');
} catch (e) {
name_of_entity = keyword;
mreid = "0";
}
return {
entity: mreid,
entity_name: name_of_entity
};
},keyword);
await page.waitFor(1000);
const entity = data_from_page.entity;
const entity_name = data_from_page.entity_name;
await page.close();
return {
entity: entity,
entity_name: entity_name
};
};
async function click_carousel_link(page,entity_link_selector) {
let response;
await page.waitFor(1000);
try {
const [response] = await Promise.all([
page.waitForNavigation({waitUntil: 'load'}),
page.click(entity_link_selector),
]);
return response;
} catch (e) {
return response = false;
}
};
async function get_entity_links(page) {
const xpath_expression_url = '//g-scrolling-carousel/div[1]/div/a/@href';
await page.waitForXPath(xpath_expression_url);
const carousel_urls = await page.$x(xpath_expression_url);
const carousel_links = await page.evaluate((...carousel_urls) => {
return carousel_urls.map(e => 'https://www.google.de'+e.nodeValue);
}, ...carousel_urls);
return carousel_links;
};
async function get_entity_name(page) {
const xpath_expression_name = '//g-scrolling-carousel/div[1]/div/a';
await page.waitForXPath(xpath_expression_name);
const carousel_names1 = await page.$x(xpath_expression_name);
const carousel_names = await page.evaluate((...carousel_names1) => {
return carousel_names1.map(e => e.innerText);
}, ...carousel_names1);
for(var i = 0; i < carousel_names.length; i++){
carousel_names[i] = carousel_names[i].replace('\n', ' ');
}
return carousel_names;
};
// ========================== Main Functions ==========================
(async function() {
const keyword = argv.kw;
let headless_arg = argv.headless;
if (headless_arg == undefined || headless_arg !== "false") {
headless_value = true;
} else if (headless_arg == "false") {
headless_value = false;
}
await puppeteer.launch({headless: headless_value, args: ['--incognito']}).then(async browser => {
entities = new Object();
let url = 'https://www.google.de/search?pws=0&no_sw_cr=1&q='+keyword;
const page = await browser.newPage();
await page.setUserAgent('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36');
await page.setViewport({
width: 1920,
height: 1080
});
const entity_1 = await go_and_extract_entity_1(browser,page,url,keyword);
console.log('Starting Point: '+entity_1.entity_name+' ('+entity_1.entity+')');
entities[entity_1.entity_name] = entity_1.entity;
const entity_link_check = await click_carousel_link(page,entity_link_selector);
if (entity_link_check == false){
console.log(entities);
console.log('No (more) Entities found!');
await page.close();
await browser.close();
process.exit(0);
}
const carousel_links = await get_entity_links(page);
const carousel_names = await get_entity_name(page);
// console.log(carousel_names);
// console.log(carousel_links);
i=0;
await asyncForEach(carousel_links, async (link) => {
await waitFor(1000);
const data_tmp = await go_and_extract_entity(browser,link,carousel_names[i]);
entities[data_tmp.entity_name] = data_tmp.entity;
console.log('New Entity found: '+data_tmp.entity_name+' ('+data_tmp.entity+')');
i++;
});
await page.waitFor(1000);
await page.close();
await browser.close();
// console.log(entities);
return entities;
});
console.log('\n');
let names = Object.keys(entities);
let mreids = Object.values(entities);
let count_entities = names.length;
let entity;
let csv_filename = 'entity_list_'+keyword+'_'+timestamp+'.csv';
console.log(count_entities+' MREIDs were collected.');
i=0;
names.forEach(name => {
entity += decodeURIComponent(name+';'+mreids[i]+'\n');
i++;
});
entity = entity.slice(9);
entity = 'Entity-Name;MREID\n'+entity;
fs.writeFile(csv_filename, entity, 'utf8', function (err) {
if (err) {
console.log('Some error occured while trying to save data to CSV!');
} else{
console.log('Data was saved successfully to CSV! ('+csv_filename+')');
}
});
})();