-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathreges_htmlparser2.py
102 lines (74 loc) · 2.34 KB
/
reges_htmlparser2.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
"""
*This section assumes that you understand the basics discussed in HTML Parser - Part 1
.handle_comment(data)
This method is called when a comment is encountered (e.g. <!--comment-->).
The data argument is the content inside the comment tag:
from HTMLParser import HTMLParser
class MyHTMLParser(HTMLParser):
def handle_comment(self, data):
print "Comment :", data
.handle_data(data)
This method is called to process arbitrary data (e.g. text nodes and the content of <script>...</script> and <style>...</style>).
The data argument is the text content of HTML.
from HTMLParser import HTMLParser
class MyHTMLParser(HTMLParser):
def handle_data(self, data):
print "Data :", data
Task
You are given an HTML code snippet of lines.
Your task is to print the single-line comments, multi-line comments and the data.
Print the result in the following format:
>>> Single-line Comment
Comment
>>> Data
My Data
>>> Multi-line Comment
Comment_multiline[0]
Comment_multiline[1]
>>> Data
My Data
>>> Single-line Comment:
Note: Do not print data if data == '\n'.
Input Format
The first line contains integer , the number of lines in the HTML code snippet.
The next lines contains HTML code.
Constraints
Output Format
Print the single-line comments, multi-line comments and the data in order of their occurrence from top to bottom in the snippet.
Format the answers as explained in the problem statement.
Sample Input
4
<!--[if IE 9]>IE9-specific content
<![endif]-->
<div> Welcome to HackerRank</div>
<!--[if IE 9]>IE9-specific content<![endif]-->
Sample Output
>>> Multi-line Comment
[if IE 9]>IE9-specific content
<![endif]
>>> Data
Welcome to HackerRank
>>> Single-line Comment
[if IE 9]>IE9-specific content<![endif]
Current Buffer (saved locally, editable)
Python 2
1
"""
import re
from HTMLParser import HTMLParser
class MyHTMLParser(HTMLParser):
def handle_comment(self, data):
if re.findall(r'\n',data):
print ">>> Multi-line Comment \n",data
else:
print ">>> Single-line Comment \n",data
def handle_data(self, data):
if len(data) >1:
print ">>> Data\n",data
html = ""
for i in range(int(raw_input())):
html += raw_input().rstrip()
html += '\n'
parser = MyHTMLParser()
parser.feed(html)
parser.close()