-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlink_finder.py
34 lines (27 loc) · 955 Bytes
/
link_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
27
28
29
30
31
32
33
34
from html.parser import HTMLParser
from urllib import parse
class LinkFinder(HTMLParser):
def __init__(self, base_url, page_url):
'''
:param base_url: the initial url of the site to crawl
:param page_url: the url of the current page of the site
'''
super().__init__()
self.base_url = base_url
self.page_url = page_url
self.links = set()
def error(self, message):
pass
def handle_starttag(self, tag, attrs):
'''
:param tag: the html tags present on the web page
:param attrs: the attributes available in the tag
:return: returns the links crawled from the page
'''
if tag == 'a':
for attribute, value in attrs:
if attribute == 'href':
url = parse.urljoin(self.base_url, value)
self.links.add(url)
def page_links(self):
return self.links