-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtml_example.py
More file actions
executable file
·69 lines (62 loc) · 1.4 KB
/
Copy pathhtml_example.py
File metadata and controls
executable file
·69 lines (62 loc) · 1.4 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
62
63
64
65
66
67
68
69
from html.parser import HTMLParser
class MyHTMLParser(HTMLParser):
def handle_starttag(self, tag, attrs):
print(f'Start : {tag}')
for atr in attrs:
print(f'-> {atr[0]} > {atr[1]}')
def handle_endtag(self, tag):
print(f'End : {tag}')
def handle_startendtag(self, tag, attrs):
print(f'Empty : {tag}')
for atr in attrs:
print(f'-> {atr[0]} > {atr[1]}')
def handle_comment(self, data):
if '\n' in data:
print(">>> Multi-line Comment")
print(data)
else:
print(f">>> Single-line Comment\n{data}")
def handle_data(self, data):
if data.strip():
print(f">>> Data\n{data}")
if __name__ == '__main__':
n = int(input())
s = ''
for i in range(n):
s += input()
parser = MyHTMLParser()
parser.feed(s)
"""
Input
2
<html><head><title>HTML Parser - I</title></head>
<body data-modal-target class='1'><h1>HackerRank</h1><br /></body></html>
Your Output (stdout)
Start : html
Start : head
Start : title
End : title
End : head
Start : body
-> data-modal-target > None
-> class > 1
Start : h1
End : h1
Empty : br
End : body
End : html
Expected Output
Start : html
Start : head
Start : title
End : title
End : head
Start : body
-> data-modal-target > None
-> class > 1
Start : h1
End : h1
Empty : br
End : body
End : html
"""