-
Notifications
You must be signed in to change notification settings - Fork 5
/
script.js
360 lines (331 loc) · 10.6 KB
/
script.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
const puppeteer = require("puppeteer");
const fs = require("fs");
const path = require("path");
const ExcelJS = require("exceljs");
const keywordsFilePath = "keywords.txt";
const searchedKeywordsFilePath = "searched_keywords.txt";
async function isBlocked(page) {
const url = page.url();
return url.toLowerCase().includes("sorry");
}
async function blockWait(page, milliseconds) {
await page.evaluate((ms) => {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}, milliseconds);
}
function findDuplicatedEmails(emailSet, email) {
return emailSet.has(email);
}
function readKeywords(filePath) {
if (fs.existsSync(filePath)) {
const data = fs.readFileSync(filePath, "utf8");
return data.split("\n").filter(Boolean);
}
return [];
}
function saveKeyword(keyword, filePath) {
fs.appendFileSync(filePath, keyword + "\n");
}
function sanitizeFilename(name) {
return name.replace(/[^a-z0-9]/gi, "_").toLowerCase();
}
async function scrapeEmailsFromGoogle(keyword) {
const browser = await puppeteer.launch({
headless: false,
args: ["--no-sandbox", "--disable-setuid-sandbox"],
defaultViewport: null,
ignoreHTTPSErrors: true,
userAgent:
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36",
});
const page = await browser.newPage();
let workbook = new ExcelJS.Workbook();
let worksheet = workbook.addWorksheet("Emails");
worksheet.columns = [{ header: "Email", key: "email", width: 50 }];
let excelFileCount = 1;
let rowCount = 0;
try {
let emailsSet = new Set();
let visitedSites = [];
let currentPage = 1;
let noMoreResults = false;
const sanitizedKeyword = sanitizeFilename(keyword);
while (!noMoreResults) {
await page.goto(
`https://www.google.com/search?q=${keyword}&start=${
(currentPage - 1) * 10
}`
);
if (await isBlocked(page)) {
console.log("Blocking detected. Waiting....");
await blockWait(page, 10000);
continue;
}
const siteURLs = await page.$$eval("div.yuRUbf a", (elements) =>
elements.map((element) => element.href)
);
if (siteURLs.length === 0) {
console.log(`No more results found for "${keyword}".`);
noMoreResults = true;
continue;
}
visitedSites.push(...siteURLs);
for (const siteURL of siteURLs) {
const restrictedSites = [
"coraltatil.com",
"jollytur.com",
".tripadvisor.com",
"https://www.trendyol.com/",
"https://otelz.com/",
"https://www.hepsiburada.com/",
"https://www.n11.com/",
"https://amazon.com.tr/",
"https://www.isbank.com.tr/",
"https://www.garantibbva.com.tr/",
"https://www.teb.com.tr/",
"https://www.akbank.com/tr-tr",
"https://www.ciceksepeti.com/",
"https://www.sahibinden.com/",
"https://www.pttavm.com/",
"https://www.odamax.com/",
];
const isRestricted = restrictedSites.some((site) =>
siteURL.includes(site)
);
if (isRestricted) {
console.log(`Access to the URL "${siteURL}" has been blocked.`);
continue;
}
try {
await page.goto(siteURL);
const content = await page.content();
const emailRegex =
/([a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,})/g;
const pageEmails = content.match(emailRegex) || [];
pageEmails.forEach(async (email) => {
const strongEmailRegex =
/([a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}(?![\w\W]*\.png$))/g;
if (strongEmailRegex.test(email)) {
const invalidExtensions = [
".png",
".webp",
".gif",
".jpg",
"donanimhaber.com",
".svg",
"etkialani.com",
".webp",
".gov.tr",
"@example.com",
"yandex-team.ru",
"firmakuruyorum.com",
"ikas.com",
"email.com",
"kuveytturk.com.tr",
"memurlar.net",
"sit.amet",
"indeed.com",
"schafer.com.tr",
"domain...vb",
"teknosa.com",
"evkur.com.tr",
"vestel.com.tr",
"adres.net",
"sabah.com.tr",
"adres.com",
"dha.com.tr",
"deneme.com",
"dha.com",
"jpg.jpeg",
"hurriyet.com.tr",
"hurriyet.com",
"ornek.com",
"tatil.com",
"evidea.com",
"@getir.com",
"domain.com",
"eczacibasi.com.tr",
"@hotmail.com",
"ntv.com.tr",
"jollytur.com",
"srvpanel.com",
"wixpress.com",
"getsentry.com",
"test.com",
"website.com",
"kelebek.com",
"site.com",
"trendyol",
"sentry.io",
"musiad.org.tr",
"musiad.at",
"muesiad.dk",
"visable.com",
"roztocil.name",
"bel.tr",
".kep.tr",
"vip.163.com",
"@donanimhaber.com",
"mudo.com.tr",
"otelfiyat.com",
"dsdamat.com.tr",
"armut.com",
"homify.com",
"yemeksepeti.com",
"11.js",
"flo.com.tr",
"littlejoyturizm.com",
"tatildenince.com",
"tatilmaximum.com.tr",
"tatilzon.com",
"myratur.com",
"amazon.com",
"coex.cz",
"nbxfx.com",
".edu.tr",
"tse.org.tr",
"eposta.com",
"kiptas.com.tr",
"verizon.net",
".yandex.ru",
"addresshere.com",
"siteadresi.com",
"eleman.net",
"emlakjet.com",
"igairport.aero",
"biletdukkani.com",
"sochic.com",
"atasay.com",
"onedio.com",
"altinbas.com",
"steinfels-kg.de",
"oggusto.com",
"youremail.com",
"boschelektriklielaletleri.com",
"tatildukkani.com",
"trthaber.com",
"sokmarket.com.tr",
"broofa.com",
"opencart.com",
"turhost.com",
"wings.com.tr",
"vanleeuwen.nl",
"otelpuan.com",
"email.here",
"chp.org.tr",
"eve.com.tr",
"yenisafak.com",
"odtuteknokent.com.tr",
"trabzonteknokent.com.tr",
"milliyet.com.tr",
"adresin.com",
"otelpuan.com.tr",
"siteniz.com",
"joomla51.com",
"odamax.com",
"osb.org.tr",
"adresiniz.com",
"osb.com",
];
const emailEndsWithInvalidExtension = invalidExtensions.some(
(ext) => email.endsWith(ext)
);
if (!emailEndsWithInvalidExtension) {
if (!findDuplicatedEmails(emailsSet, email)) {
emailsSet.add(email);
const emailFilePath = path.join(
__dirname,
`${sanitizedKeyword}.txt`
);
fs.appendFileSync(emailFilePath, email + "\n");
worksheet.addRow({ email: email });
rowCount++;
if (rowCount >= 300) {
await workbook.xlsx.writeFile(
path.join(
__dirname,
`${sanitizedKeyword}_emails_${excelFileCount}.xlsx`
)
);
excelFileCount++;
workbook = new ExcelJS.Workbook();
worksheet = workbook.addWorksheet("Emails");
worksheet.columns = [
{ header: "Email", key: "email", width: 50 },
];
rowCount = 0;
}
}
}
}
});
} catch (error) {
console.error(`Error accessing the site: ${siteURL}`, error);
continue;
}
}
currentPage++;
}
if (rowCount > 0) {
await workbook.xlsx.writeFile(
path.join(
__dirname,
`${sanitizedKeyword}_emails_${excelFileCount}.xlsx`
)
);
}
const emails = [...emailsSet];
return { emails, visitedSites };
} catch (error) {
throw error;
} finally {
await browser.close();
}
}
async function scrapeEmailsForMultipleKeywords() {
const keywords = readKeywords(keywordsFilePath);
const searchedKeywords = readKeywords(searchedKeywordsFilePath);
for (const keyword of keywords) {
if (searchedKeywords.includes(keyword)) {
console.log(
`The keyword "${keyword}" has already been searched. Skipping...`
);
continue;
}
try {
console.log(
`Searching for email addresses for the keyword "${keyword}"...`
);
const { emails, visitedSites } = await scrapeEmailsFromGoogle(keyword);
console.log(
`Email addresses for the keyword "${keyword}" have been successfully retrieved.`
);
if (visitedSites.length === 0) {
console.log(visitedSites, "empty");
console.log(
`No results found for "${keyword}". Moving on to the next keyword.`
);
continue;
}
saveKeyword(keyword, searchedKeywordsFilePath);
} catch (error) {
console.error(`An error occurred for the keyword "${keyword}":`, error);
}
}
}
scrapeEmailsForMultipleKeywords()
.then(() => {
console.log(
"Email addresses for all keywords have been successfully retrieved."
);
})
.catch((error) => {
console.error("Error:", error);
});