-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlink_finder.py
34 lines (30 loc) · 1012 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 __future__ import print_function
from HTMLParser import HTMLParser
from urllib2 import Request, urlopen
from urlparse import urljoin
class LinkFinder(HTMLParser):
def handle_starttag(self, tag, attrs):
if tag == 'a':
for (key, value) in attrs:
if key == 'href':
newUrl = urljoin(self.baseUrl, value)
self.links = self.links + [newUrl]
def getLinks(self, url):
self.links = []
self.baseUrl = url
response = urlopen(url)
try:
htmlBytes = response.read()
htmlString = htmlBytes.decode("utf-8")
self.feed(htmlString)
return list(set(self.links))
except Exception as e:
print(e)
return []
def spider(url, maxPages):
parser = LinkFinder()
for link in parser.getLinks(url=url):
print(link)
if __name__ == "__main__":
## running ...
spider(url="http://eu.battle.net/wow/en/", maxPages=10)