-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
Create emails_from_url.py #1756
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
0f96b74
Create emails_from_url.py
lablnet 090cd66
Update emails_from_url.py
itsvinayak f86fd16
Update emails_from_url.py
itsvinayak df2938d
0 emails found:
cclauss 13ec4e5
Update emails_from_url.py
lablnet a0af0fd
Use Python set() to remove duplicates
cclauss 47d15bb
Update emails_from_url.py
cclauss f0f0118
Add type hints and doctests
cclauss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
"""Get the site emails from URL.""" | ||
__author__ = "Muhammad Umer Farooq" | ||
__license__ = "MIT" | ||
__version__ = "1.0.0" | ||
__maintainer__ = "Muhammad Umer Farooq" | ||
__email__ = "[email protected]" | ||
__status__ = "Alpha" | ||
|
||
import re | ||
from html.parser import HTMLParser | ||
from urllib import parse | ||
|
||
import requests | ||
|
||
|
||
class Parser(HTMLParser): | ||
def __init__(self, domain: str): | ||
HTMLParser.__init__(self) | ||
self.data = [] | ||
self.domain = domain | ||
|
||
def handle_starttag(self, tag: str, attrs: str) -> None: | ||
""" | ||
This function parse html to take takes url from tags | ||
""" | ||
# Only parse the 'anchor' tag. | ||
if tag == "a": | ||
# Check the list of defined attributes. | ||
for name, value in attrs: | ||
# If href is defined, and not empty nor # print it. | ||
if name == "href" and value != "#" and value != "": | ||
# If not already in data. | ||
if value not in self.data: | ||
url = parse.urljoin(self.domain, value) | ||
self.data.append(url) | ||
|
||
|
||
# Get main domain name (example.com) | ||
def get_domain_name(url: str) -> str: | ||
""" | ||
This function get the main domain name | ||
|
||
>>> get_domain_name("https://a.b.c.d/e/f?g=h,i=j#k") | ||
'c.d' | ||
>>> get_domain_name("Not a URL!") | ||
'' | ||
""" | ||
return ".".join(get_sub_domain_name(url).split(".")[-2:]) | ||
|
||
|
||
# Get sub domain name (sub.example.com) | ||
def get_sub_domain_name(url: str) -> str: | ||
""" | ||
This function get sub domin name | ||
|
||
>>> get_sub_domain_name("https://a.b.c.d/e/f?g=h,i=j#k") | ||
'a.b.c.d' | ||
>>> get_sub_domain_name("Not a URL!") | ||
'' | ||
""" | ||
return parse.urlparse(url).netloc | ||
|
||
|
||
def emails_from_url(url: str = "https://github.com") -> list: | ||
""" | ||
This function takes url and return all valid urls | ||
""" | ||
# Get the base domain from the url | ||
domain = get_domain_name(url) | ||
|
||
# Initialize the parser | ||
parser = Parser(domain) | ||
|
||
try: | ||
# Open URL | ||
r = requests.get(url) | ||
|
||
# pass the raw HTML to the parser to get links | ||
parser.feed(r.text) | ||
|
||
# Get links and loop through | ||
valid_emails = set() | ||
for link in parser.data: | ||
# open URL. | ||
# read = requests.get(link) | ||
try: | ||
read = requests.get(link) | ||
# Get the valid email. | ||
emails = re.findall("[a-zA-Z0-9]+@" + domain, read.text) | ||
# If not in list then append it. | ||
for email in emails: | ||
valid_emails.add(email) | ||
except ValueError: | ||
pass | ||
except ValueError: | ||
exit(-1) | ||
|
||
# Finally return a sorted list of email addresses with no duplicates. | ||
return sorted(valid_emails) | ||
|
||
|
||
if __name__ == "__main__": | ||
emails = emails_from_url("https://github.com") | ||
print(f"{len(emails)} emails found:") | ||
print("\n".join(sorted(emails))) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.