-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
65 lines (59 loc) · 1.75 KB
/
main.py
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
import requests
import re
import sys
from bs4 import BeautifulSoup
from urllib.parse import urlparse
def get_domain_name(url):
try:
results = get_sub_domain_name(url).split('.')
return results[-2] + '.' + results[-1]
except:
return ''
def get_sub_domain_name(url):
try:
return urlparse(url).netloc
except:
return ''
def get_emails(url):
try:
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
emails = set(re.findall(r"[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+", soup.text))
return emails
except:
return set()
def get_links(url):
try:
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
links = set()
for link in soup.find_all('a', href=True):
links.add(link['href'])
return links
except:
return set()
def get_emails_from_domain(url):
domain_name = get_domain_name(url)
if domain_name == '':
print('Invalid URL')
return
emails = set()
links = set()
links.add(url)
while len(links) > 0:
link = links.pop()
if get_domain_name(link) == domain_name:
emails = emails.union(get_emails(link))
links = links.union(get_links(link))
return emails
if __name__ == '__main__':
url = input('Enter the URL: ')
if not url.startswith('http'):
url = 'http://' + url
if not re.match(r'https?://(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)', url):
print('Invalid URL')
sys.exit()
emails = get_emails_from_domain(url)
for email in emails:
print(email)
print('Total emails found:', len(emails))