1"""
2This module defines the `main()` coroutine for the Apify Actor, executed from the `__main__.py` file.
3
4Feel free to modify this file to suit your specific needs.
5
6To build Apify Actors, utilize the Apify SDK toolkit, read more at the official documentation:
7https://docs.apify.com/sdk/python
8"""
9
10from urllib.parse import urljoin
11from selenium.webdriver.common.action_chains import ActionChains
12from apify import Actor
13import re
14from urllib.parse import urljoin
15from bs4 import BeautifulSoup
16import requests
17import requests.exceptions
18import time
19import random
20from .datascrapify import StartProcess, parse_proxy_url,on_aborting_event
21from apify_shared.consts import ActorEventTypes as Event
22import asyncio
23from urllib.parse import urlparse, urlsplit, urlunsplit, parse_qs, urlencode
24import os
25
26
27
28
29
30
31def scrape_contact_emails(link):
32 res = requests.get(link)
33 domain = link.split(".")
34 mailaddr = link
35 soup = BeautifulSoup(res.text,"lxml")
36 links = soup.find_all("a")
37 contact_link = ''
38 final_result = ""
39 try:
40
41 emails = soup.find_all(text=re.compile('.*@'+domain[1]+'.'+domain[2].replace("/","")))
42 emails.sort(key=len)
43 print(emails[0].replace("\n",""))
44 final_result = emails[0]
45 except:
46
47 try:
48 flag = 0
49 for link in links:
50 if "contact" in link.get("href") or "Contact" in link.get("href") or "CONTACT" in link.get("href") or 'contact' in link.text or 'Contact' in link.text or 'CONTACT' in link.text:
51 if len(link.get("href"))>2 and flag<2:
52 flag = flag + 1
53 contact_link = link.get("href")
54
55 except:
56 pass
57
58 domain = domain[0]+"."+domain[1]+"."+domain[2]
59 if(len(contact_link)<len(domain)):
60 domain = domain+contact_link.replace("/","")
61 else:
62 domain = contact_link
63
64 try:
65
66 res = requests.get(domain)
67 soup = BeautifulSoup(res.text,"lxml")
68 emails = soup.find_all(text=re.compile('.*@'+mailaddr[7:].replace("/","")))
69 emails.sort(key=len)
70 try:
71 print(emails[0].replace("\n",""))
72 final_result = emails[0]
73 return final_result
74 except:
75 pass
76 except Exception as e:
77 pass
78
79 return ""
80
81async def main() -> None:
82 """
83 The main coroutine is being executed using `asyncio.run()`, so do not attempt to make a normal function
84 out of it, it will not work. Asynchronous execution is required for communication with Apify platform,
85 and it also enhances performance in the field of web scraping significantly.
86 """
87 async with Actor:
88
89 actor_input = await Actor.get_input() or {}
90 Keyword_val = actor_input.get('Keyword')
91 location_val = actor_input.get('location')
92 social_network_val = actor_input.get('social_network')
93 Country_val = actor_input.get('Country')
94 Email_Type_val = actor_input.get('Email_Type')
95 Other_Email_Type_val = actor_input.get('Other_Email_Type')
96 proxy_settings = actor_input.get('proxySettings')
97 Limit_val =actor_input.get('Limit')
98
99
100 proxy_configuration = await Actor.create_proxy_configuration(groups=['GOOGLE_SERP'])
101
102 proxyurl =await proxy_configuration.new_url()
103 if proxy_configuration and proxy_settings:
104 proxyurl =await proxy_configuration.new_url()
105
106
107
108 if not Keyword_val:
109 Actor.log.info('Please insert keyword')
110 await Actor.push_data({'Email': 'Please insert keyword'})
111 await Actor.exit()
112 return
113
114 if Keyword_val=='TestKeyword':
115 Actor.log.info('Please insert keyword')
116 await Actor.push_data({'Email': 'Please insert Your Keyword'})
117 await Actor.exit()
118 return
119
120 is_paying = os.getenv("APIFY_USER_IS_PAYING")
121 print(is_paying)
122 if is_paying=="1":
123 print('Paid User')
124 else:
125 Limit_val=10
126 isPaying='FreeUser'
127 await Actor.push_data({'Email': 'Free User Can Get Only 10 Records'})
128
129 '''
130 try:
131 proxyurl=''
132 USE_Proxy=False
133 if proxyurl:
134 USE_Proxy=True
135 my_proxy_settings = parse_proxy_url(proxyurl)
136 # Call the function from the imported module
137 await StartProcess(
138 "Apify_camp_LinkedinEmail_"+username+"_"+isPaying,
139 "ALLINONE",
140 Keyword_val,
141 location_val,
142 social_network_val,
143 Country_val,
144 "Google",
145 USE_Proxy,
146 my_proxy_settings
147 )
148
149 finally:
150 # Code that always executes (e.g., cleanup)
151 print("This block always runs, regardless of exceptions.")
152
153 await Actor.exit();
154 '''
155
156 l1 = ["it","www","gm","fr","sp","uk","al","ag","ar","am","as","au","aj","bg","bo","be","bh","bk","br","bu","ca","ci","ch","co","cs","hr","cu","ez","dk","ec","eg","en","fi","gg","gr","hk","hu","ic","in","id","ir","iz","ei","is","jm","ja","ke","kn","ks","ku","lg","ly","ls","lh","lu","mc","mk","my","mt","mx","md","mn","mj","mo","np","nl","nz","ni","no","pk","we","pm","pa","pe","rp","pl","po","rq","qa","ro","rs","sm","sa","sg","ri","sn","lo","si","sf","sw","sz","sy","tw","th","ts","tu","ua","ae","uy","uz","ve"]
157 l2= ["Italy","United States","Germany","France","Spain","United Kingdom","Albania","Algeria","Argentina","Armenia","Australia","Austria","Azerbaijan","Bangladesh","Belarus","Belgium","Belize","Bosnia and Herzegovina","Brazil","Bulgaria","Canada","Chile","China","Colombia","Costa Rica","Croatia","Cuba","Czechia","Denmark","Ecuador","Egypt","Estonia","Finland","Georgia","Greece","Hong Kong","Hungary","Iceland","India","Indonesia","Iran","Iraq","Ireland","Israel","Jamaica","Japan","Kenya","Korea","Korea, Republic of","Kuwait","Latvia","Libya","Liechtenstein","Lithuania","Luxembourg","Macao","Macedonia","Malaysia","Malta","Mexico","Moldova, Republic of","Monaco","Montenegro","Morocco","Nepal","Netherlands","New Zealand","Nigeria","Norway","Pakistan","Palestine, State of","Panama","Paraguay","Peru","Philippines","Poland","Portugal","Puerto Rico","Qatar","Romania","Russia","San Marino","Saudi Arabia","Senegal","Serbia","Singapore","Slovakia","Slovenia","South Africa","Sweden","Switzerland","Syrian Arab Republic","Taiwan","Thailand","Tunisia","Turkey","Ukraine","United Arab Emirates","Uruguay","Uzbekistan","Venezuela"]
158 select_index=1
159 select_country='United States'
160 for count, ele in enumerate(l1):
161 if(ele==Country_val):
162 select_index=count
163 break
164
165
166
167 for count, ele in enumerate(l2):
168 if(count==select_index):
169 select_country=ele
170 break
171
172 print(select_country)
173
174 concatstring = ""
175 concatstring = concatstring + Keyword_val
176 option = "( @gmail.com OR @hotmail.com OR @yahoo.com)";
177 if Email_Type_val=="1":
178 if not Other_Email_Type_val:
179 Actor.log.info('Please insert Email Type Domain')
180 await Actor.push_data({'Email': 'Please insert Email Type Domain'})
181 await Actor.exit()
182 return
183 if Other_Email_Type_val.find("@") > -1:
184 option = " ( " + Other_Email_Type_val + " )"
185 else:
186 option = " ( @" + Other_Email_Type_val + " )"
187 concatstring = concatstring + option
188 if location_val:
189 concatstring = concatstring+ " in "+ location_val
190
191
192
193 if social_network_val:
194 concatstring = concatstring + " site:"
195
196 if social_network_val == "linkedin.com/" or social_network_val == "pinterest.com/" :
197 concatstring = concatstring + Country_val + ".";
198
199
200
201
202 if social_network_val == "amazon.com/" :
203 if Country_val=='gm':
204 Country_val='de'
205 elif Country_val=='sp':
206 Country_val='es'
207 elif Country_val=='fr':
208 Country_val='fr'
209 elif Country_val=='uk':
210 Country_val='co.uk'
211 elif Country_val=='as':
212 Country_val='com.au'
213 elif Country_val=='www':
214 Country_val='com'
215 elif Country_val=='in':
216 Country_val='in'
217 elif Country_val=='be':
218 Country_val='com.be'
219 elif Country_val=='br':
220 Country_val='com.br'
221 elif Country_val=='ca':
222 Country_val='ca'
223 elif Country_val=='ch':
224 Country_val='cn'
225 elif Country_val=='eg':
226 Country_val='eg'
227 elif Country_val=='it':
228 Country_val='it'
229 elif Country_val=='ja':
230 Country_val='co.jp'
231 elif Country_val=='mx':
232 Country_val='com.mx'
233 elif Country_val=='nl':
234 Country_val='nl'
235 elif Country_val=='pl':
236 Country_val='pl'
237 elif Country_val=='sa':
238 Country_val='sa'
239 elif Country_val=='sn':
240 Country_val='sg'
241 elif Country_val=='sw':
242 Country_val='se'
243 elif Country_val=='tu':
244 Country_val='com.tr'
245 elif Country_val=='ae':
246 Country_val='ae'
247 elif Country_val=='ae':
248 Country_val='ae'
249 else :
250 Country_val=='com'
251
252 social_network_val=social_network_val.replace('.com','.'+Country_val)
253
254
255 concatstring = concatstring + "" + social_network_val + "";
256
257 SearchEngine='Google'
258 desktop_user_agents = ["Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36",
259 "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36",
260 "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.6167.139 Safari/537.36",
261 "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
262
263 "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:125.0) Gecko/20100101 Firefox/125.0",
264 "Mozilla/5.0 (Windows NT 10.0; rv:124.0) Gecko/20100101 Firefox/124.0",
265 "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:122.0) Gecko/20100101 Firefox/122.0",
266
267 "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.6312.86 Safari/537.36 Edg/123.0.2420.65",
268 "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.6261.112 Safari/537.36 Edg/122.0.2365.66",
269
270 "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36 OPR/96.0.0.0",
271
272 "Mozilla/5.0 (Macintosh; Intel Mac OS X 13_5_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.6261.129 Safari/537.36",
273 "Mozilla/5.0 (Macintosh; Intel Mac OS X 12_6_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36",
274
275 "Mozilla/5.0 (Macintosh; Intel Mac OS X 13.5; rv:124.0) Gecko/20100101 Firefox/124.0",
276 "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:123.0) Gecko/20100101 Firefox/123.0",
277
278 "Mozilla/5.0 (Macintosh; Intel Mac OS X 13_4) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.4 Safari/605.1.15",
279 "Mozilla/5.0 (Macintosh; Intel Mac OS X 12_3_1) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.4 Safari/605.1.15",
280
281 "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.6167.184 Safari/537.36",
282 "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
283
284 "Mozilla/5.0 (X11; Linux x86_64; rv:125.0) Gecko/20100101 Firefox/125.0",
285 "Mozilla/5.0 (X11; Linux x86_64; rv:124.0) Gecko/20100101 Firefox/124.0",
286 "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:123.0) Gecko/20100101 Firefox/123.0",
287
288 "Mozilla/5.0 (Macintosh; Intel Mac OS X 13_2_1) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.3 Safari/605.1.15",
289
290 "Mozilla/5.0 (Macintosh; Intel Mac OS X 13_4_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.6312.86 Safari/537.36 Edg/123.0.2420.65",
291
292 "Mozilla/5.0 (Macintosh; Intel Mac OS X 13_2_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36 OPR/96.0.0.0"
293 ]
294
295 mobile_user_agents = [
296
297 "Mozilla/5.0 (Linux; Android 13; Pixel 7 Pro) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.6312.105 Mobile Safari/537.36",
298 "Mozilla/5.0 (Linux; Android 12; Pixel 6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Mobile Safari/537.36",
299 "Mozilla/5.0 (Linux; Android 11; Pixel 4a) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.6167.140 Mobile Safari/537.36",
300
301
302 "Mozilla/5.0 (Linux; Android 13; SAMSUNG SM-S911B) AppleWebKit/537.36 (KHTML, like Gecko) SamsungBrowser/24.0 Chrome/123.0.0.0 Mobile Safari/537.36",
303 "Mozilla/5.0 (Linux; Android 12; SAMSUNG SM-A525F) AppleWebKit/537.36 (KHTML, like Gecko) SamsungBrowser/23.0 Chrome/121.0.0.0 Mobile Safari/537.36",
304
305
306 "Mozilla/5.0 (Linux; Android 13; M2101K6G) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.6312.105 Mobile Safari/537.36",
307
308
309 "Mozilla/5.0 (iPhone; CPU iPhone OS 17_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.0 Mobile/15E148 Safari/604.1",
310 "Mozilla/5.0 (iPhone; CPU iPhone OS 16_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.0 Mobile/15E148 Safari/604.1",
311 "Mozilla/5.0 (iPhone; CPU iPhone OS 15_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.5 Mobile/15E148 Safari/604.1",
312
313
314 "Mozilla/5.0 (iPad; CPU OS 17_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.0 Mobile/15E148 Safari/604.1",
315 "Mozilla/5.0 (iPad; CPU OS 15_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.0 Mobile/15E148 Safari/604.1",
316
317
318 "Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like Mac OS X) AppleWebKit/537.36 (KHTML, like Gecko) CriOS/123.0.0.0 Mobile/15E148 Safari/604.1",
319
320
321 "Mozilla/5.0 (Linux; Android 13; Pixel 6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Mobile Safari/537.36 OPR/76.0.4017.123",
322
323
324 "Mozilla/5.0 (Linux; Android 13; Pixel 6 Pro) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Mobile Safari/537.36 EdgA/123.0.2420.64",
325
326
327 "Mozilla/5.0 (Android 13; Mobile; rv:124.0) Gecko/124.0 Firefox/124.0",
328 "Mozilla/5.0 (Android 12; Mobile; rv:122.0) Gecko/122.0 Firefox/122.0"
329]
330
331
332 all_user_agents = desktop_user_agents + mobile_user_agents
333 random_user_agent = random.choice(all_user_agents)
334
335
336 qry="https://google.com/search?q="+ concatstring
337 Actor.log.info(concatstring)
338
339 gl_map = {
340 "it": "it",
341 "www": "us",
342 "gm": "de",
343 "fr": "fr",
344 "sp": "es",
345 "uk": "uk",
346 "al": "al",
347 "ag": "dz",
348 "ar": "ar",
349 "am": "am",
350 "as": "au",
351 "au": "at",
352 "aj": "az",
353 "bg": "bd",
354 "bo": "by",
355 "be": "be",
356 "bh": "bz",
357 "bk": "ba",
358 "br": "br",
359 "bu": "bg",
360 "ca": "ca",
361 "ci": "cl",
362 "ch": "cn",
363 "co": "co",
364 "cs": "cr",
365 "hr": "hr",
366 "cu": "cu",
367 "ez": "ez",
368 "dk": "dk",
369 "ec": "ec",
370 "eg": "eg",
371 "en": "ee",
372 "fi": "fi",
373 "gg": "ge",
374 "gr": "gr",
375 "hk": "hk",
376 "hu": "hu",
377 "ic": "is",
378 "in": "in",
379 "id": "id",
380 "ir": "ir",
381 "iz": "iq",
382 "ei": "ie",
383 "is": "il",
384 "jm": "jm",
385 "ja": "jp",
386
387 "ke": "ke",
388 "kn": "kp",
389 "ks": "kr",
390 "ku": "kw",
391 "lg": "lv",
392 "ly": "ly",
393 "ls": "li",
394 "lh": "lt",
395 "lu": "lu",
396 "mc": "mo",
397 "mk": "mk",
398 "my": "my",
399 "mt": "mt",
400 "mx": "mx",
401 "md": "md",
402 "mn": "mc",
403 "mj": "me",
404 "mo": "ma",
405 "np": "np",
406 "nl": "nl",
407 "nz": "nz",
408 "ni": "ng",
409 "no": "no",
410 "pk": "pk",
411 "we": "ps",
412 "pm": "pa",
413 "pa": "py",
414 "pe": "pe",
415 "rp": "ph",
416 "pl": "pl",
417 "po": "pt",
418 "rq": "pr",
419 "qa": "qa",
420 "ro": "ro",
421 "rs": "ru",
422 "sm": "sm",
423 "sa": "sa",
424 "sg": "sn",
425 "ri": "rs",
426 "sn": "sg",
427 "lo": "sk",
428 "si": "si",
429 "sf": "za",
430 "sw": "se",
431 "sz": "ch",
432 "sy": "sy",
433 "tw": "tw",
434 "th": "th",
435 "ts": "tn",
436 "tu": "tu",
437 "ua": "ua",
438 "ae": "ae",
439 "uy": "uy",
440 "uz": "uz",
441 "ve": "ve",
442 "vn": "vn",
443 "lk": "lk",
444 }
445
446 gl = gl_map.get(Country_val, Country_val)
447
448
449
450 all_users = []
451 query =concatstring
452 max_pages = 100
453 results = []
454 count_result=0
455 has_next=True
456 for page in range(max_pages):
457 try:
458 start = page * 10
459 Actor.log.info('Check Page '+str(page))
460 url = f"http://www.google.com/search?q={query}&num=10&hl=en&start={start}&gl={gl}"
461
462
463
464 proxies=None
465 if proxyurl:
466 proxies = {'http': proxyurl, 'https': proxyurl}
467
468 response = requests.get(
469 url,
470 proxies=proxies,
471 headers={
472 "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
473 "AppleWebKit/537.36 (KHTML, like Gecko) "
474 "Chrome/120.0.0.0 Safari/537.36"
475 }
476 )
477
478 if response.status_code != 200:
479 Actor.log.warning(f"⚠️ Request failed: {response.status_code} {response.reason}")
480 continue
481
482 soup = BeautifulSoup(response.text, "html.parser")
483 result_blocks = soup.select("div.g, div.tF2Cxc")
484
485 for j, block in enumerate(result_blocks):
486 Email=''
487 title_el = block.select_one("h3")
488 link_el = block.select_one("a")
489 snippet_el = block.select_one(".VwiC3b, .IsZvec, .aCOpRe")
490
491
492
493 title= title_el.get_text(strip=True)
494 url =link_el.get("href")
495 snippet= snippet_el.get_text(strip=True)
496 print(title)
497 alltext = title+snippet
498 match = re.findall(r'[a-zA-Z0-9\.\-+_]+@[a-zA-Z0-9\.\-+_]+\.[a-zA-Z]+', alltext)
499 Actor.log.info('match email '+str(len(match)))
500 if len(match)>0:
501 for i in match:
502 Email=i
503 Actor.log.info('email '+Email)
504 else:
505 match_website = re.findall(r'\\b(?:https?://|www\\.)\\S+\\b', alltext)
506 for i in match_website:
507 Website = "http://www."+i;
508 Actor.log.info('Website '+Website)
509 Email=scrape_contact_emails(Website)
510 if Email :
511 existindb=False
512
513 if len(all_users)>0:
514 for item in all_users:
515 if item['Email'] == Email :
516 existindb=True
517 break
518
519 if existindb==False:
520 all_users.append({'Email': Email});
521 await Actor.push_data({'Email': Email, 'title': title,'Description':alltext,'Detail_Link':url})
522 count_result=count_result+1
523 if(Limit_val):
524 if Limit_val!='0':
525 if(count_result>=int(Limit_val)):
526 has_next = False
527 print('Limit Exceed')
528 break
529
530 if has_next==False:
531 break
532 await asyncio.sleep(random.uniform(1.5, 3.0))
533 except:
534 break
535
536
537 print('Check in Yahoo Search Engine')
538 query=concatstring.replace("OR", "or")
539
540
541 for page in range(max_pages):
542 try:
543 if has_next==False:
544 break
545 start = page * 10
546 Actor.log.info('Check Page '+str(page))
547
548 url = "https://search.yahoo.com/search;_ylt=Awr.2lgGCQ9p1C0DekZXNyoA;_ylu=Y29sbwNncTEEcG9zAzEEdnRpZAMEc2VjA3BhZ2luYXRpb24-"
549 params = {
550 "p": query,
551 "b": start,
552 "pz": 10,
553 "bct": 0,
554 "xargs": 0
555 }
556
557
558 split_url = urlsplit(url)
559 query_str = urlencode(params)
560 full_url = urlunsplit((split_url.scheme, split_url.netloc, split_url.path, query_str, ""))
561
562 print(f"Fetching page {full_url}")
563
564
565 proxies=None
566
567
568
569 response = requests.get(
570 full_url,
571 proxies=proxies,
572 headers={
573 "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
574 "AppleWebKit/537.36 (KHTML, like Gecko) "
575 "Chrome/120.0.0.0 Safari/537.36"
576 }
577 )
578
579 if response.status_code != 200:
580 Actor.log.warning(f"⚠️ Request failed: {response.status_code} {response.reason}")
581 break
582
583 soup = BeautifulSoup(response.text, "html.parser")
584 search_results = soup.select("div.dd.algo.algo-sr")
585
586 if not search_results:
587 log("⚠️ No more results found, ending.")
588 break
589
590 for r in search_results:
591 try:
592 Email=''
593 Website=''
594 Address=''
595 BusinessName=''
596 DetailsLink=''
597 Category=''
598 link_tag = r.select_one("h3.title")
599 link_tag1 = r.select_one("a")
600 if link_tag:
601 BusinessName = link_tag.get_text(strip=True)
602 DetailsLink = link_tag1.get("href")
603 Category = urlparse(DetailsLink).netloc
604
605 addr_tag = r.select_one("div.compText.aAbs")
606 if addr_tag:
607 Address = addr_tag.get_text(separator=" ").strip()
608
609 combined = r.get_text(strip=True)
610 print(combined)
611 match = re.findall(r'[a-zA-Z0-9\.\-+_]+@[a-zA-Z0-9\.\-+_]+\.[a-zA-Z]+', combined)
612 Actor.log.info('match email '+str(len(match)))
613 if len(match)>0:
614 for i in match:
615 Email=i
616 Actor.log.info('email '+Email)
617 else:
618 match_website = re.findall(r'\\b(?:https?://|www\\.)\\S+\\b', combined)
619 for i in match_website:
620 Website = "http://www."+i;
621 Actor.log.info('Website '+Website)
622 Email=scrape_contact_emails(Website)
623 if Email :
624 existindb=False
625
626 if len(all_users)>0:
627 for item in all_users:
628 if item['Email'] == Email :
629 existindb=True
630 break
631
632 if existindb==False:
633 all_users.append({'Email': Email});
634 await Actor.push_data({'Email': Email, 'title': BusinessName,'Description':combined,'Detail_Link':DetailsLink})
635 count_result=count_result+1
636 if(Limit_val):
637 if Limit_val!='0':
638 if(count_result>=int(Limit_val)):
639 has_next = False
640 print('Limit Exceed')
641 break
642
643 except Exception as ex:
644 print(f"⚠️ Parse error: {ex}")
645 continue
646
647 if has_next==False:
648 break
649 await asyncio.sleep(random.uniform(1.5, 3.0))
650 except:
651
652 break
653 await Actor.exit()
654