-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse_html.py
More file actions
55 lines (42 loc) · 1.57 KB
/
parse_html.py
File metadata and controls
55 lines (42 loc) · 1.57 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
from html.parser import HTMLParser
meta_count = 0
class MyHTMLParser(HTMLParser):
def error(self, message):
pass
def handle_comment(self, data):
# called when comment is encountered
print("Encountered comment", data)
pos = self.getpos() # returns line and position of comment
print("\tAt line:", pos[0], "position", pos[1])
def handle_starttag(self, tag, attrs):
# function called when entire stag tag is read including attributes
global meta_count
if tag == 'meta':
meta_count += 1
print("Encountered start tag", tag)
pos = self.getpos()
# returns line and position of comment
print("\tAt line:", pos[0], "position", pos[1])
if attrs.__len__() > 0:
print("\tAtttributes:")
for a in attrs:
print("\t", a[0], "=", a[1])
def handle_endtag(self, tag):
print("Encountered end tag", tag)
pos = self.getpos() # returns line and position of comment
print("\tAt line:", pos[0], "position", pos[1])
def handle_data(self, data):
if data.isspace():
return
print("Encountered data", data)
pos = self.getpos() # returns line and position of comment
print("\tAt line:", pos[0], "position", pos[1])
def main():
parser = MyHTMLParser()
f = open("samplehtml.html")
if f.mode == 'r': # file successfully opened
contents = f.read()
parser.feed(contents)
print("Meta tags found", meta_count)
if __name__ == '__main__':
main()