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
11
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
21import asyncio
22from urllib.parse import urlparse, urlsplit, urlunsplit, parse_qs, urlencode
23import os
24from .playwritescraper import callpython
25
26
27
28
29import datetime
30
31async def check_free_trial(user_id: str, is_paid_user: bool = False):
32 """
33 Returns True if the user is allowed to use the Actor.
34
35 Paid users: always allowed.
36 Free users: allowed for 24 hours from first use.
37 """
38
39
40 if is_paid_user:
41 return True
42
43 store = await Actor.open_key_value_store(name="user-trials-linkedinemailscrapers")
44
45 trial_key = f"trial_start_{user_id}"
46
47 now = datetime.datetime.now(datetime.timezone.utc)
48
49 first_use = await store.get_value(trial_key)
50
51
52 if not first_use:
53 await store.set_value(
54 trial_key,
55 now.isoformat()
56 )
57
58 Actor.log.info(
59 f"Free trial started for user {user_id}. "
60 f"Trial valid for 24 hours."
61 )
62
63 return True
64
65
66 first_use_date = datetime.datetime.fromisoformat(first_use)
67
68
69 if first_use_date.tzinfo is None:
70 first_use_date = first_use_date.replace(
71 tzinfo=datetime.timezone.utc
72 )
73
74 elapsed = now - first_use_date
75
76 if elapsed < datetime.timedelta(hours=24):
77 remaining = datetime.timedelta(hours=24) - elapsed
78
79 hours = int(remaining.total_seconds() // 3600)
80 minutes = int((remaining.total_seconds() % 3600) // 60)
81
82 Actor.log.info(
83 f"Free trial active. "
84 f"Remaining: {hours}h {minutes}m"
85 )
86
87 return True
88
89 Actor.log.warning(
90 f"Free trial expired for user {user_id}"
91 )
92
93 return False
94
95def scrape_contact_emails(link):
96 res = requests.get(link)
97 domain = link.split(".")
98 mailaddr = link
99 soup = BeautifulSoup(res.text,"lxml")
100 links = soup.find_all("a")
101 contact_link = ''
102 final_result = ""
103 try:
104
105 emails = soup.find_all(text=re.compile('.*@'+domain[1]+'.'+domain[2].replace("/","")))
106 emails.sort(key=len)
107 print(emails[0].replace("\n",""))
108 final_result = emails[0]
109 except:
110
111 try:
112 flag = 0
113 for link in links:
114 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:
115 if len(link.get("href"))>2 and flag<2:
116 flag = flag + 1
117 contact_link = link.get("href")
118
119 except:
120 pass
121
122 domain = domain[0]+"."+domain[1]+"."+domain[2]
123 if(len(contact_link)<len(domain)):
124 domain = domain+contact_link.replace("/","")
125 else:
126 domain = contact_link
127
128 try:
129
130 res = requests.get(domain)
131 soup = BeautifulSoup(res.text,"lxml")
132 emails = soup.find_all(text=re.compile('.*@'+mailaddr[7:].replace("/","")))
133 emails.sort(key=len)
134 try:
135 print(emails[0].replace("\n",""))
136 final_result = emails[0]
137 return final_result
138 except:
139 pass
140 except Exception as e:
141 pass
142
143 return ""
144
145async def main() -> None:
146 """
147 The main coroutine is being executed using `asyncio.run()`, so do not attempt to make a normal function
148 out of it, it will not work. Asynchronous execution is required for communication with Apify platform,
149 and it also enhances performance in the field of web scraping significantly.
150 """
151 async with Actor:
152
153 actor_input = await Actor.get_input() or {}
154 Keyword_val = actor_input.get('Keyword')
155 location_val = actor_input.get('location')
156 social_network_val = actor_input.get('social_network')
157 Country_val = actor_input.get('Country')
158 Email_Type_val = actor_input.get('Email_Type')
159 Other_Email_Type_val = actor_input.get('Other_Email_Type')
160 proxy_settings = actor_input.get('proxySettings')
161 Limit_val =actor_input.get('Limit')
162
163 proxy_configuration_selected = await Actor.create_proxy_configuration(actor_proxy_input=proxy_settings)
164 proxyurl_selected =None
165 if proxy_configuration_selected and proxy_settings:
166 proxyurl_selected =await proxy_configuration_selected.new_url()
167 print(proxyurl_selected)
168
169 proxy_configuration = await Actor.create_proxy_configuration(groups=['GOOGLE_SERP'])
170
171 proxyurl =await proxy_configuration.new_url()
172 if proxy_configuration and proxy_settings:
173 proxyurl =await proxy_configuration.new_url()
174
175
176
177 if not Keyword_val:
178 Actor.log.info('Please insert keyword')
179 await Actor.push_data({'Email': 'Please insert keyword'})
180 await Actor.exit()
181 return
182
183 if Keyword_val=='TestKeyword':
184 Actor.log.info('Please insert keyword')
185 await Actor.push_data({'Email': 'Please insert Your Keyword'})
186 await Actor.exit()
187 return
188
189 is_paying = os.getenv("APIFY_USER_IS_PAYING")
190 print(is_paying)
191 if is_paying=="1":
192 print('Paid User')
193 else:
194 Limit_val=0
195 is_paid_user=False
196 APIFY_USER_ID = os.getenv("APIFY_USER_ID")
197 allowed = await check_free_trial(user_id=APIFY_USER_ID, is_paid_user=is_paid_user)
198 if not allowed:
199 await Actor.push_data({'Email': 'Free User can run for One Day Only. Please Subcribe'})
200 return
201
202
203 '''
204 try:
205 proxyurl=''
206 USE_Proxy=False
207 if proxyurl:
208 USE_Proxy=True
209 my_proxy_settings = parse_proxy_url(proxyurl)
210 # Call the function from the imported module
211 await StartProcess(
212 "Apify_camp_LinkedinEmail_"+username+"_"+isPaying,
213 "ALLINONE",
214 Keyword_val,
215 location_val,
216 social_network_val,
217 Country_val,
218 "Google",
219 USE_Proxy,
220 my_proxy_settings
221 )
222
223 finally:
224 # Code that always executes (e.g., cleanup)
225 print("This block always runs, regardless of exceptions.")
226
227 await Actor.exit();
228 '''
229
230 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"]
231 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"]
232 select_index=1
233 select_country='United States'
234 for count, ele in enumerate(l1):
235 if(ele==Country_val):
236 select_index=count
237 break
238
239
240
241 for count, ele in enumerate(l2):
242 if(count==select_index):
243 select_country=ele
244 break
245
246 print(select_country)
247
248 concatstring = ""
249 concatstring = concatstring + Keyword_val
250 option = "( @gmail.com OR @hotmail.com OR @yahoo.com)";
251 if Email_Type_val=="1":
252 if not Other_Email_Type_val:
253 Actor.log.info('Please insert Email Type Domain')
254 await Actor.push_data({'Email': 'Please insert Email Type Domain'})
255 await Actor.exit()
256 return
257 if Other_Email_Type_val.find("@") > -1:
258 option = " ( " + Other_Email_Type_val + " )"
259 else:
260 option = " ( @" + Other_Email_Type_val + " )"
261 concatstring = concatstring + option
262 if location_val:
263 concatstring = concatstring+ " in "+ location_val
264
265
266
267 if social_network_val:
268 concatstring = concatstring + " site:"
269
270 if social_network_val == "linkedin.com/" or social_network_val == "pinterest.com/" :
271 concatstring = concatstring + Country_val + ".";
272
273
274
275
276 if social_network_val == "amazon.com/" :
277 if Country_val=='gm':
278 Country_val='de'
279 elif Country_val=='sp':
280 Country_val='es'
281 elif Country_val=='fr':
282 Country_val='fr'
283 elif Country_val=='uk':
284 Country_val='co.uk'
285 elif Country_val=='as':
286 Country_val='com.au'
287 elif Country_val=='www':
288 Country_val='com'
289 elif Country_val=='in':
290 Country_val='in'
291 elif Country_val=='be':
292 Country_val='com.be'
293 elif Country_val=='br':
294 Country_val='com.br'
295 elif Country_val=='ca':
296 Country_val='ca'
297 elif Country_val=='ch':
298 Country_val='cn'
299 elif Country_val=='eg':
300 Country_val='eg'
301 elif Country_val=='it':
302 Country_val='it'
303 elif Country_val=='ja':
304 Country_val='co.jp'
305 elif Country_val=='mx':
306 Country_val='com.mx'
307 elif Country_val=='nl':
308 Country_val='nl'
309 elif Country_val=='pl':
310 Country_val='pl'
311 elif Country_val=='sa':
312 Country_val='sa'
313 elif Country_val=='sn':
314 Country_val='sg'
315 elif Country_val=='sw':
316 Country_val='se'
317 elif Country_val=='tu':
318 Country_val='com.tr'
319 elif Country_val=='ae':
320 Country_val='ae'
321 elif Country_val=='ae':
322 Country_val='ae'
323 else :
324 Country_val=='com'
325
326 social_network_val=social_network_val.replace('.com','.'+Country_val)
327
328
329 concatstring = concatstring + "" + social_network_val + "";
330
331 SearchEngine='Google'
332 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",
333 "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36",
334 "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.6167.139 Safari/537.36",
335 "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
336
337 "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:125.0) Gecko/20100101 Firefox/125.0",
338 "Mozilla/5.0 (Windows NT 10.0; rv:124.0) Gecko/20100101 Firefox/124.0",
339 "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:122.0) Gecko/20100101 Firefox/122.0",
340
341 "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",
342 "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",
343
344 "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",
345
346 "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",
347 "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",
348
349 "Mozilla/5.0 (Macintosh; Intel Mac OS X 13.5; rv:124.0) Gecko/20100101 Firefox/124.0",
350 "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:123.0) Gecko/20100101 Firefox/123.0",
351
352 "Mozilla/5.0 (Macintosh; Intel Mac OS X 13_4) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.4 Safari/605.1.15",
353 "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",
354
355 "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.6167.184 Safari/537.36",
356 "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
357
358 "Mozilla/5.0 (X11; Linux x86_64; rv:125.0) Gecko/20100101 Firefox/125.0",
359 "Mozilla/5.0 (X11; Linux x86_64; rv:124.0) Gecko/20100101 Firefox/124.0",
360 "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:123.0) Gecko/20100101 Firefox/123.0",
361
362 "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",
363
364 "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",
365
366 "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"
367 ]
368
369 mobile_user_agents = [
370
371 "Mozilla/5.0 (Linux; Android 13; Pixel 7 Pro) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.6312.105 Mobile Safari/537.36",
372 "Mozilla/5.0 (Linux; Android 12; Pixel 6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Mobile Safari/537.36",
373 "Mozilla/5.0 (Linux; Android 11; Pixel 4a) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.6167.140 Mobile Safari/537.36",
374
375
376 "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",
377 "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",
378
379
380 "Mozilla/5.0 (Linux; Android 13; M2101K6G) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.6312.105 Mobile Safari/537.36",
381
382
383 "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",
384 "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",
385 "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",
386
387
388 "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",
389 "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",
390
391
392 "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",
393
394
395 "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",
396
397
398 "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",
399
400
401 "Mozilla/5.0 (Android 13; Mobile; rv:124.0) Gecko/124.0 Firefox/124.0",
402 "Mozilla/5.0 (Android 12; Mobile; rv:122.0) Gecko/122.0 Firefox/122.0"
403]
404
405
406 all_user_agents = desktop_user_agents + mobile_user_agents
407 random_user_agent = random.choice(all_user_agents)
408
409
410 qry="https://google.com/search?q="+ concatstring
411 Actor.log.info(concatstring)
412
413 gl_map = {
414 "it": "it",
415 "www": "us",
416 "gm": "de",
417 "fr": "fr",
418 "sp": "es",
419 "uk": "uk",
420 "al": "al",
421 "ag": "dz",
422 "ar": "ar",
423 "am": "am",
424 "as": "au",
425 "au": "at",
426 "aj": "az",
427 "bg": "bd",
428 "bo": "by",
429 "be": "be",
430 "bh": "bz",
431 "bk": "ba",
432 "br": "br",
433 "bu": "bg",
434 "ca": "ca",
435 "ci": "cl",
436 "ch": "cn",
437 "co": "co",
438 "cs": "cr",
439 "hr": "hr",
440 "cu": "cu",
441 "ez": "ez",
442 "dk": "dk",
443 "ec": "ec",
444 "eg": "eg",
445 "en": "ee",
446 "fi": "fi",
447 "gg": "ge",
448 "gr": "gr",
449 "hk": "hk",
450 "hu": "hu",
451 "ic": "is",
452 "in": "in",
453 "id": "id",
454 "ir": "ir",
455 "iz": "iq",
456 "ei": "ie",
457 "is": "il",
458 "jm": "jm",
459 "ja": "jp",
460
461 "ke": "ke",
462 "kn": "kp",
463 "ks": "kr",
464 "ku": "kw",
465 "lg": "lv",
466 "ly": "ly",
467 "ls": "li",
468 "lh": "lt",
469 "lu": "lu",
470 "mc": "mo",
471 "mk": "mk",
472 "my": "my",
473 "mt": "mt",
474 "mx": "mx",
475 "md": "md",
476 "mn": "mc",
477 "mj": "me",
478 "mo": "ma",
479 "np": "np",
480 "nl": "nl",
481 "nz": "nz",
482 "ni": "ng",
483 "no": "no",
484 "pk": "pk",
485 "we": "ps",
486 "pm": "pa",
487 "pa": "py",
488 "pe": "pe",
489 "rp": "ph",
490 "pl": "pl",
491 "po": "pt",
492 "rq": "pr",
493 "qa": "qa",
494 "ro": "ro",
495 "rs": "ru",
496 "sm": "sm",
497 "sa": "sa",
498 "sg": "sn",
499 "ri": "rs",
500 "sn": "sg",
501 "lo": "sk",
502 "si": "si",
503 "sf": "za",
504 "sw": "se",
505 "sz": "ch",
506 "sy": "sy",
507 "tw": "tw",
508 "th": "th",
509 "ts": "tn",
510 "tu": "tu",
511 "ua": "ua",
512 "ae": "ae",
513 "uy": "uy",
514 "uz": "uz",
515 "ve": "ve",
516 "vn": "vn",
517 "lk": "lk",
518 }
519
520 gl = gl_map.get(Country_val, Country_val)
521
522
523
524 all_users = []
525 query =concatstring
526 max_pages = 100
527 results = []
528 count_result=0
529 has_next=True
530
531
532 try_count=0
533 page=0
534
535 while page < max_pages:
536 try :
537 page=page+1
538 Actor.log.info('Check Next Page'+str(page))
539 start = page * 10
540 url = f"http://www.google.com/search?q={query}&num=10&hl=en&start={start}"
541
542
543
544 proxies=None
545 if proxyurl:
546 proxies = {'http': proxyurl, 'https': proxyurl}
547
548 response = requests.get(
549 url,
550 proxies=proxies,
551 headers={
552 "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
553 "AppleWebKit/537.36 (KHTML, like Gecko) "
554 "Chrome/120.0.0.0 Safari/537.36"
555 }
556 )
557
558 if response.status_code != 200:
559 Actor.log.warning(f"⚠️ Request failed: {response.status_code} {response.reason}")
560 continue
561
562 soup = BeautifulSoup(response.text, "html.parser")
563 result_blocks = soup.select("div.g, div.tF2Cxc")
564 if(len(result_blocks)==0):
565 print('No Result')
566 page=page-1
567 try_count=try_count+1
568 if try_count>2:
569 break;
570 continue
571 if(len(result_blocks)>0):
572 try_count=0
573 for j, block in enumerate(result_blocks):
574 Email=''
575 title_el = block.select_one("h3")
576 link_el = block.select_one("a")
577 snippet_el = block.select_one(".VwiC3b, .IsZvec, .aCOpRe")
578
579
580
581 title= title_el.get_text(strip=True)
582 url =link_el.get("href")
583 if "/goto" in url:
584 url='https://google.com/'+url
585 try:
586 r = requests.get(url)
587 print(r.url)
588 url=r.url
589 except:
590 pass
591 snippet= snippet_el.get_text(strip=True)
592 print(title)
593 alltext = title+snippet
594 match = re.findall(r'[a-zA-Z0-9\.\-+_]+@[a-zA-Z0-9\.\-+_]+\.[a-zA-Z]+', alltext)
595 Actor.log.info('match email '+str(len(match)))
596 if len(match)>0:
597 for i in match:
598 Email=i
599 Actor.log.info('email '+Email)
600 else:
601 match_website = re.findall(r'\\b(?:https?://|www\\.)\\S+\\b', alltext)
602 for i in match_website:
603 Website = "http://www."+i;
604 Actor.log.info('Website '+Website)
605 Email=scrape_contact_emails(Website)
606 if Email :
607 existindb=False
608
609 if len(all_users)>0:
610 for item in all_users:
611 if item['Email'] == Email :
612 existindb=True
613 break
614
615 if existindb==False:
616 all_users.append({'Email': Email});
617 await Actor.push_data({'Email': Email, 'title': title,'Description':alltext,'Detail_Link':url})
618 count_result=count_result+1
619 if(Limit_val):
620 if Limit_val!='0':
621 if(count_result>=int(Limit_val)):
622 has_next = False
623 print('Limit Exceed')
624 break
625
626 if has_next==False:
627 break
628 await asyncio.sleep(random.uniform(1.5, 3.0))
629 except Exception as e:
630 print(e)
631 continue
632
633
634 print('Check in Yahoo Search Engine')
635 query=concatstring.replace("OR", "or")
636
637
638 for page in range(max_pages):
639 try:
640 if has_next==False:
641 break
642 start = page * 10
643 Actor.log.info('Check Page '+str(page))
644
645 url = "https://search.yahoo.com/search"
646 params = {
647 "p": query,
648 "b": start,
649 "pz": 10,
650 "bct": 0,
651 "xargs": 0
652 }
653
654
655 split_url = urlsplit(url)
656 query_str = urlencode(params)
657 full_url = urlunsplit((split_url.scheme, split_url.netloc, split_url.path, query_str, ""))
658
659
660
661
662 proxies=None
663 if proxyurl_selected:
664 proxies = {'http': proxyurl_selected, 'https': proxyurl_selected}
665
666 response = requests.get(
667 full_url,
668 proxies=proxies,
669 headers={
670 "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
671 "AppleWebKit/537.36 (KHTML, like Gecko) "
672 "Chrome/120.0.0.0 Safari/537.36"
673 }
674 )
675
676 if response.status_code != 200:
677
678 break
679
680 soup = BeautifulSoup(response.text, "html.parser")
681 search_results = soup.select("div.dd.algo.algo-sr")
682 print(search_results)
683 if not search_results:
684 log("⚠️ No more results found, ending.")
685 break
686 if(len(search_results)==0):
687 print('No Result')
688 break;
689
690 for j, r in enumerate(search_results):
691 try:
692 Email=''
693 Website=''
694 Address=''
695 BusinessName=''
696 DetailsLink=''
697 Category=''
698 link_tag = r.select_one("h3.title")
699
700 link_tag1 = r.select_one("a")
701 if link_tag:
702 BusinessName = link_tag.get_text(strip=True)
703 print(BusinessName)
704 DetailsLink = link_tag1.get("href")
705 Category = urlparse(DetailsLink).netloc
706 addr_tag = r.select_one("div.compText.aAbs")
707 if addr_tag:
708 Address = addr_tag.get_text(separator=" ").strip()
709 combined = r.get_text(strip=True)
710 print(combined)
711 match = re.findall(r'[a-zA-Z0-9\.\-+_]+@[a-zA-Z0-9\.\-+_]+\.[a-zA-Z]+', combined)
712 Actor.log.info('match email '+str(len(match)))
713 if len(match)>0:
714 for i in match:
715 Email=i
716 Actor.log.info('email '+Email)
717 else:
718 match_website = re.findall(r'\\b(?:https?://|www\\.)\\S+\\b', combined)
719 for i in match_website:
720 Website = "http://www."+i;
721 Actor.log.info('Website '+Website)
722 Email=scrape_contact_emails(Website)
723 if Email :
724 existindb=False
725
726 if len(all_users)>0:
727 for item in all_users:
728 if item['Email'] == Email :
729 existindb=True
730 break
731
732 if existindb==False:
733 all_users.append({'Email': Email});
734 await Actor.push_data({'Email': Email, 'title': BusinessName,'Description':combined,'Detail_Link':DetailsLink})
735 count_result=count_result+1
736 if(Limit_val):
737 if Limit_val!='0':
738 if(count_result>=int(Limit_val)):
739 has_next = False
740 print('Limit Exceed')
741 break
742
743 except Exception as ex:
744 print(f"⚠️ Parse error: {ex}")
745 continue
746
747 if has_next==False:
748 break
749 await asyncio.sleep(random.uniform(1.5, 3.0))
750 except:
751
752 break
753
754
755 if is_paying=="1":
756 print('Start from Method 3')
757 try:
758 APIFY_USER_ID = os.getenv("APIFY_USER_ID")
759 proxyurl=''
760 USE_Proxy=False
761 my_proxy_settings=None
762
763 qry="https://google.com/search?q="+ concatstring+"&gl="+gl
764 Actor.log.info(concatstring)
765 await callpython(qry,all_users,social_network_val,count_result,Limit_val)
766
767 '''
768 await StartProcess(
769 "Apify_camp_linkedin_"+APIFY_USER_ID,
770 "ALLINONE",
771 Keyword_val,
772 location_val,
773 social_network_val,
774 Country_val,
775 "Google",
776 USE_Proxy,
777 my_proxy_settings,all_users,count_result,Limit_val
778 )
779 '''
780 finally:
781 await on_aborting_event()
782
783