MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / emails_from_url

Function emails_from_url

web_programming/emails_from_url.py:70–111  ·  view source on GitHub ↗

This function takes url and return all valid urls

(url: str = "https://github.com")

Source from the content-addressed store, hash-verified

68
69
70def emails_from_url(url: str = "https://github.com") -> list[str]:
71 """
72 This function takes url and return all valid urls
73 """
74 # Get the base domain from the url
75 domain = get_domain_name(url)
76
77 # Initialize the parser
78 parser = Parser(domain)
79
80 try:
81 # Open URL
82 r = httpx.get(url, timeout=10, follow_redirects=True)
83
84 # pass the raw HTML to the parser to get links
85 parser.feed(r.text)
86
87 # Get links and loop through
88 valid_emails = set()
89 for link in parser.urls:
90 # open URL.
91 # Check if the link is already absolute
92 if not link.startswith("http://") and not link.startswith("https://"):
93 # Prepend protocol only if link starts with domain, normalize otherwise
94 if link.startswith(domain):
95 link = f"https://{link}"
96 else:
97 link = parse.urljoin(f"https://{domain}", link)
98 try:
99 read = httpx.get(link, timeout=10, follow_redirects=True)
100 # Get the valid email.
101 emails = re.findall("[a-zA-Z0-9]+@" + domain, read.text)
102 # If not in list then append it.
103 for email in emails:
104 valid_emails.add(email)
105 except ValueError:
106 pass
107 except ValueError:
108 raise SystemExit(1)
109
110 # Finally return a sorted list of email addresses with no duplicates.
111 return sorted(valid_emails)
112
113
114if __name__ == "__main__":

Callers 1

emails_from_url.pyFile · 0.85

Calls 4

get_domain_nameFunction · 0.85
ParserClass · 0.85
getMethod · 0.45
addMethod · 0.45

Tested by

no test coverage detected