-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhtmldet.py
70 lines (55 loc) · 1.5 KB
/
htmldet.py
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
example_html = '''
<html>
<head>
<title>HTML Parser - I</title>
</head>
<body data-modal-target class='1'>
<h1 class="header">HackerRank</h1>
<br id="main"/>
<!--[if IE 9]>IE9-specific content
<![endif]-->
<div> Welcome to HackerRank</div>
<!--[if IE 9]>IE9-specific content<![endif]-->
</body>
</html>
'''
class MyHTMLParser(HTMLParser):
def handle_starttag(self, tag, attrs):
print(f'{tag}')
for k, v in attrs:
print(f'-> {k} > {v}')
def handle_endtag(self, tag):
# print(f'End : {tag}')
pass
# Empty tags:
def handle_startendtag(self, tag, attrs):
self.handle_starttag(tag, attrs)
def handle_data(self, data):
'''
if data.strip():
# print(f'Encountered some data: {data}')
print(f'>>> Data\n{data}')
'''
pass
def handle_comment(self, data):
'''
if data.strip():
lines = data.splitlines()
if len(lines) == 1:
# print(f'Encountered a comment: {data}')
print(f'>>> Single-line Comment\n{data}')
else:
print(f'>>> Multi-line Comment\n{data}')
'''
pass
def main():
parser = MyHTMLParser()
lines = int(input())
data = '\n'.join(input() for _ in range(lines))
parser.feed(data)
parser.close()
# Example:
# parser.feed(example_html)
if __name__ == '__main__':
main()