-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsandbox.py
More file actions
executable file
·47 lines (36 loc) · 1.14 KB
/
sandbox.py
File metadata and controls
executable file
·47 lines (36 loc) · 1.14 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
#!/usr/bin/env python2.7
import urllib2
from HTMLParser import HTMLParser
def getHTML(url):
response = urllib2.urlopen(url)
html = response.read()
return html
class MyHTMLParser(HTMLParser, object):
tags = {}
def __init__(self):
super(MyHTMLParser, self).__init__()
def handle_starttag(self, tag, attrs):
print "<", tag,">"
if tag in self.tags:
self.tags[tag] += 1
else:
self.tags[tag] = 1
def handle_endtag(self, tag):
print "</", tag, ">"
def handle_data(self, data):
pass
def printTags(self):
print "# of tags: %d" % len(self.tags)
print self.tags
print "Top 5 tags: "
topFive = [v[0] for v in sorted(self.tags.iteritems(), key=lambda(k, v): (-v, k))]
for tag in topFive[:5]:
print "%s: %d ocurrences" % (tag, self.tags[tag])
def main():
#sys.argv = [sys.argv[0]]+["--open_browser", "default_browser"]+sys.argv[1:]
html = getHTML("http://ordergroove.com/company")
myParser = MyHTMLParser()
myParser.feed(html)
myParser.printTags()
if __name__ == '__main__':
main()