-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWeb Crawl.py
More file actions
47 lines (43 loc) · 1.54 KB
/
Copy pathWeb Crawl.py
File metadata and controls
47 lines (43 loc) · 1.54 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
from html.parser import HTMLParser
from urllib.request import urlopen
from urllib import parse
class parseLink(HTMLParser):
def tag_handle(self,tag,att):
if tag == 'a':
for(key,value) in att:
if key == 'href':
newURL = parse.urljoin(self.baseUrl,value)
self.links = self.links + [newUrl]
def getLinks(self, url):
self.links = []
self.baseUrl = url
response = urlopen(url)
if response.getheader('Content-Type')=='text/html':
htmlBytes = response.read()
htmlString = htmlBytes.decode("utf-8")
self.feed(htmlString)
return htmlString, self.links
else:
return "",[]
def spider(url,word,maxPages):
pagestoVisit = [url]
numberVisited = 0
foundWord = False
while numberVisited < maxPages and pagestoVisit !=[] and not foundWord:
numberVisited = numberVisited +1
url = pagestoVisit[0]
pagestoVisit = pagestoVisit[1:]
try:
print(numberVisited, "Visiting:", url)
parser = parseLink()
data, links = parser.getLinks(url)
if data.find(word)>-1:
foundWord = True
pagestoVisit = pagestoVisit + links
print("Sucess")
except:
print("Failed")
if foundWord:
print("The word",word,"was found at", url)
else:
print("Word not found")