-
Notifications
You must be signed in to change notification settings - Fork 1
/
finder.py
26 lines (20 loc) · 854 Bytes
/
finder.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
from html.parser import HTMLParser
from urllib import parse
# Doc: https://docs.python.org/3/library/html.parser.html
class LinkFinder(HTMLParser):
def __init__(self, base_url, page_url):
super().__init__()
self.base_url = base_url
self.page_url = page_url
self.links = set()
# When we call HTMLParser feed() this function is called when it encounters an opening tag <a>
def handle_starttag(self, tag, attr):
if tag == 'a': # Only handle anchor element <a> tag
for (attribute, value) in attr:
if attribute == 'href':
url = parse.urljoin(self.base_url, value) # Add the base url if the url found is relative url
self.links.add(url)
def page_links(self):
return self.links
def error(self, message):
pass