-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebscraper.py
37 lines (29 loc) · 1.07 KB
/
webscraper.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
35
36
import urllib2
import operator
from HTMLParser import HTMLParser
class HTMLTagCounter(HTMLParser):
tags = {}
def handle_starttag(self, tag, attrs):
if tag not in self.tags:
self.tags[tag] = 1
else:
self.tags[tag] = self.tags[tag] + 1
def get_html_source(self,URL):
request = urllib2.Request(URL, headers={'User-Agent' : "Browser"})
page = urllib2.urlopen(request)
return page.read()
def sort_tags(self):
return sorted(self.tags.items(), key=operator.itemgetter(1), reverse=True)
def top_five(self):
sorted_tags = self.sort_tags()
print "the 5 most used tags are:"
for i in range(0,5):
print sorted_tags[i]
def total_number_of_elements(self):
return len(self.tags)
if __name__ == "__main__":
counter = HTMLTagCounter()
source = counter.get_html_source("http://ordergroove.com/company")
counter.feed(source)
print "The page has a total number of " + str(counter.total_number_of_elements()) + " elements"
counter.top_five()