-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathget_paas.js
331 lines (239 loc) · 11.8 KB
/
get_paas.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
const puppeteer = require('puppeteer');
const argv = require('minimist')(process.argv.slice(2));
const fs = require('fs');
// ========================== Declare Variables ==========================
let time = new Date().toLocaleString("de-DE",{year: 'numeric', month: '2-digit', day: '2-digit'});
let timestamp = new Date().getTime();
let paas_result = new Array();
let url;
const google_url_de = 'https://www.google.com/search?hl=de&gl=DE&ie=utf-8&oe=utf-8&no_sw_cr=1&pws=0&q=';
const google_url_us = 'https://www.google.com/search?hl=en&gl=US&ie=utf-8&oe=utf-8&no_sw_cr=1&pws=0&q=';
const keyword = argv.kw;
const clicks = argv.clicks;
const output = argv.output;
const language = argv.lang;
const help = argv.help;
// ========================== 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 get_paas(browser,keyword,clicks){
if (language == "en"){
url = google_url_us+keyword;
} else {
url = google_url_de+keyword;
}
let pages = await browser.pages();
const page = pages[0];
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: 'networkidle0'});
if (clicks == 0) {
const xpath_expression_question = '//div/div/g-accordion-expander/div[1]/div';
await page.waitForXPath(xpath_expression_question);
const questions_without_click = await page.$x(xpath_expression_question);
const count_questions = questions_without_click.length;
const questions = await page.evaluate((...questions_without_click) => {
return questions_without_click.map(e => e.innerText);
}, ...questions_without_click);
return {
questions: questions,
count: count_questions
};
} else if (clicks == 1) {
const xpath_expression_question = '//div/div/g-accordion-expander/div[1]/div';
await page.waitForXPath(xpath_expression_question);
await page.evaluate(() => {document.getElementsByClassName("g kno-kp mnr-c g-blk")[0].scrollIntoView();});
const questions_without_click = await page.$x(xpath_expression_question);
let counter = 0;
if (questions_without_click.length > 0) {
for(i in questions_without_click) {
await questions_without_click[i].click();
await page.waitFor(1000);
counter++;
};
}
console.log(counter+" questions clicked.");
const questions_with_1_click = await page.$x(xpath_expression_question);
const count_questions = questions_with_1_click.length;
const questions = await page.evaluate((...questions_with_1_click) => {
return questions_with_1_click.map(e => e.innerText);
}, ...questions_with_1_click);
return {
questions: questions,
count: count_questions
};
} else if (clicks == 2) {
const xpath_expression_question = '//div/div/g-accordion-expander/div[1]/div';
await page.waitForXPath(xpath_expression_question);
await page.evaluate(() => {document.getElementsByClassName("g kno-kp mnr-c g-blk")[0].scrollIntoView();});
const questions_without_click = await page.$x(xpath_expression_question);
let counter = 0;
if (questions_without_click.length > 0) {
for(i in questions_without_click) {
await questions_without_click[i].click();
await page.waitFor(1000);
counter++;
};
}
console.log(counter+" questions clicked.");
await page.waitFor(1000);
const questions_with_1_click = await page.$x(xpath_expression_question);
await page.waitFor(1000);
await page.evaluate(() => {document.getElementsByClassName("duf3")[1].scrollIntoView();});
if (questions_with_1_click.length > counter) {
for(counter; counter < questions_with_1_click.length; counter++) {
await questions_with_1_click[counter].click();
await page.waitFor(1000);
};
}
console.log(counter+" questions clicked.");
await page.waitFor(1000);
const questions_with_2_click = await page.$x(xpath_expression_question);
const count_questions = questions_with_2_click.length;
await page.waitFor(1000);
const questions = await page.evaluate((...questions_with_2_click) => {
return questions_with_2_click.map(e => e.innerText);
}, ...questions_with_2_click);
return {
questions: questions,
count: count_questions
};
} else if (clicks == "max") {
// max = 4 rounds if clicks
const xpath_expression_question = '//div/div/g-accordion-expander/div[1]/div';
await page.waitForXPath(xpath_expression_question);
await page.evaluate(() => {document.getElementsByClassName("g kno-kp mnr-c g-blk")[0].scrollIntoView();});
const questions_without_click = await page.$x(xpath_expression_question);
let counter = 0;
if (questions_without_click.length > 0) {
for(i in questions_without_click) {
await questions_without_click[i].click();
await page.waitFor(800);
counter++;
};
}
console.log(counter+" questions clicked.");
await page.waitFor(500);
const questions_with_1_click = await page.$x(xpath_expression_question);
await page.waitFor(500);
await page.evaluate(() => {document.getElementsByClassName("duf3")[1].scrollIntoView();});
if (questions_with_1_click.length > counter) {
for(counter; counter < questions_with_1_click.length; counter++) {
await questions_with_1_click[counter].click();
await page.waitFor(800);
};
}
console.log(counter+" questions clicked.");
await page.waitFor(500);
const questions_with_2_click = await page.$x(xpath_expression_question);
await page.waitFor(500);
await page.evaluate(() => {document.getElementsByClassName("duf3")[1].scrollIntoView();});
if (questions_with_2_click.length > counter) {
for(counter; counter < questions_with_2_click.length; counter++) {
await questions_with_2_click[counter].click();
await page.waitFor(800);
};
}
console.log(counter+" questions clicked.");
await page.waitFor(500);
const questions_with_3_click = await page.$x(xpath_expression_question);
await page.waitFor(500);
await page.evaluate(() => {document.getElementsByClassName("duf3")[1].scrollIntoView();});
if (questions_with_3_click.length > counter) {
for(counter; counter < questions_with_3_click.length; counter++) {
await questions_with_3_click[counter].click();
await page.waitFor(800);
};
}
console.log(counter+" questions clicked.");
await page.waitFor(500);
const questions_with_4_click = await page.$x(xpath_expression_question);
const count_questions = questions_with_4_click.length;
await page.waitFor(500);
const questions = await page.evaluate((...questions_with_4_click) => {
return questions_with_4_click.map(e => e.innerText);
}, ...questions_with_4_click);
return {
questions: questions,
count: count_questions
};
}
await page.close();
};
// ========================== Main Functions ==========================
(async function(){
if (help) {
console.log("\n👨🎓 How to use the script.\n");
console.log("You can run the script by typing 'npm run scraper' or 'node get_paas.js' followed by arguments.");
console.log("Arguments are:");
console.log(" ♦ '--clicks=[0-2/max]' : how often click on new questions");
console.log(" ♦ '--kw=[...]' : input of keyword (search term) or 'keywords' for batch mode (read line by line from `keywords.txt`)");
console.log(" ♦ '--lang=[de/en]' : input of languange");
console.log(" ♦ '--output=csv' : (optional) to export list of questions");
console.log(" ♦ '--help' : help");
console.log("\n📋 Examples:\n");
console.log("♦ npm run scraper -- --clicks=0 --kw=angela+merkel --lang=de");
console.log("♦ node get_paas.js --clicks=0 --kw=angela+merkel --lang=de\n");
console.log("♦ npm run scraper -- --clicks=max --kw=firefox --output=csv --lang=en");
console.log("♦ node get_paas.js --clicks=max --kw=firefox --output=csv --lang=en");
console.log("\n☀ For further examples or issues go to https://github.com/jpigla/PAAs-from-SERPs.");
} else {
try {
await puppeteer.launch({headless: true}).then(async browser => {
if (keyword == "keywords") {
let lineReader = require('readline').createInterface({
input: require('fs').createReadStream('keywords.txt')
});
for await (const keywords of lineReader) {
const result = await get_paas(browser,keywords,clicks);
console.log('');
paas_result.push('== Search term: '+keywords+' ('+result.count+') ==');
await asyncForEach(result.questions, async (question) => {
paas_result.push(question);
});
paas_result.push('');
// paas_result.push('== Number of questions found: '+result.count+' ==');
};
} else {
const result = await get_paas(browser,keyword,clicks);
console.log('');
paas_result.push('== Search term: '+keyword+' ('+result.count+') ==');
await asyncForEach(result.questions, async (question) => {
paas_result.push(question);
});
// paas_result.push('== Number of questions found: '+result.count+' ==');
}
paas_result.forEach(question => {
console.log(question);
});
if (output == "csv") {
let csv_output;
let csv_filename = 'paas_'+keyword.replace(" ","+")+'_'+timestamp+'.csv';
paas_result.pop();
paas_result.forEach(question => {
csv_output += decodeURIComponent(question+'\n');
});
csv_output = csv_output.slice(9);
fs.writeFile(csv_filename, csv_output, 'utf8', function (err) {
if (err) {
console.log('Some error occured while trying to save data to CSV!');
} else{
console.log('\nData was saved successfully to CSV! ('+csv_filename+')');
}
});
}
await browser.close();
});
} catch(e) {
console.log(e)
process.exit()
}
}
})();