-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHTMLparse.py
More file actions
61 lines (52 loc) · 1.44 KB
/
HTMLparse.py
File metadata and controls
61 lines (52 loc) · 1.44 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
51
52
53
54
55
56
57
58
59
60
61
import time
from HTMLParser import HTMLParser
from htmlentitydefs import name2codepoint
class MyHTMLParser(HTMLParser):
def handle_starttag(self, tag, attrs):
global stack
for i in range(stack):
print(' '),
stack+=1
print('<%s, attrs:%s>' % (tag, str(attrs)))
def handle_endtag(self, tag):
global stack
stack-=1
for i in range(stack):
print(' '),
print('</%s>' % tag)
def handle_startendtag(self, tag, attrs):
global stack
for i in range(stack):
print(' '),
print('<%s/, attrs:%s>' % (tag, str(attrs)))
def handle_data(self, data):
global stack
for i in range(stack):
print(' '),
print(data)
def handle_comment(self, data):
global stack
for i in range(stack):
print(' '),
print('<!-- -->')
def handle_entityref(self, name):
global stack
for i in range(stack):
print(' '),
print('&%s;' % name)
def handle_charref(self, name):
global stack
for i in range(stack):
print(' '),
print('&#%s;' % name)
stack = 0
parser = MyHTMLParser()
#parser.feed('<html><head></head><body><p>Some <a href=\"#\">html</a> tutorial...<br/>END</p></body></html>')
filename=('latest.xml')
f=open(filename)
while True:
l=f.readline()
if not l:
break
parser.feed(l)
f.close()