-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInspectorGadget5.py
More file actions
51 lines (36 loc) · 1.13 KB
/
Copy pathInspectorGadget5.py
File metadata and controls
51 lines (36 loc) · 1.13 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
48
49
50
# -*- coding: utf-8 -*-
# <nbformat>3.0</nbformat>
# <markdowncell>
# # InspectorGadget
# #### a class to parse HTML documents and find stuff
# <codecell>
import urllib2
from HTMLParser import HTMLParser
# <codecell>
class InspectorGadget(HTMLParser, object):
commentlist = []
taglist = []
def __init__(self, doc=None, url=None):
super(InspectorGadget, self).__init__()
if url:
page = urllib2.urlopen(url)
doc = page.read()
if doc:
super(InspectorGadget, self).feed(doc)
else:
return
def handle_comment(self, data):
self.save_comment(data)
print "Found %s comment(s)!" % len(self.commentlist)
def handle_starttag(self, tag, attrs):
self.save_tag(tag, attrs)
print "Found tag: %s" % tag
def save_comment(self, data):
self.commentlist.append(data)
return
def save_tag(self, tag, attrs):
self.taglist.append({tag: attrs})
def get_comments(self):
return self.commentlist
def get_tags(self):
return self.taglist