Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
You are given an HTML code snippet of N lines.
Your task is to detect and print all the HTML tags, attributes and attribute values.
Print the detected items in the following format:
Tag1
Tag2
-> Attribute2[0] > Attribute_value2[0]
-> Attribute2[1] > Attribute_value2[1]
-> Attribute2[2] > Attribute_value2[2]
Tag3
-> Attribute3[0] > Attribute_value3[0]
The -> symbol indicates that the tag contains an attribute. It is immediately followed by the name of the attribute and the attribute value.
The > symbol acts as a separator of attributes and attribute values.
If an HTML tag has no attribute then simply print the name of the tag.
Note: Do not detect any HTML tag, attribute or attribute value inside the HTML comment tags (<!– Comments –>). Comments can be multiline.
All attributes have an attribute value.
The first line contains an integer N, the number of lines in the HTML code snippet.
The next N lines contain HTML code.
Print the HTML tags, attributes and attribute values in order of their occurrence from top to bottom in the snippet.
Format your answers as explained in the problem statement.
9
<head>
<title>HTML</title>
</head>
<object type="application/x-flash"
data="your-file.swf"
width="0" height="0">
<!-- <param name="movie" value="your-file.swf" /> -->
<param name="quality" value="high"/>
</object>
head
title
object
-> type > application/x-flash
-> data > your-file.swf
-> width > 0
-> height > 0
param
-> name > quality
-> value > high
import re
num_lines = input()
lines = [raw_input() for i in xrange(num_lines)]
text = ' '.join(lines)
text = re.sub(r'<!--.*?-->', '', text, flags=re.DOTALL)
from HTMLParser import HTMLParser
class Parser(HTMLParser):
def handle_starttag(self, tag, attrs):
print tag
for name, val in attrs:
print '->', name, '>', val
Parser().feed(text)
# Enter your code here. Read input from STDIN. Print output to STDOUT
from html.parser import HTMLParser
class MyHTMLParser(HTMLParser):
def handle_starttag(self, tag, attrs):
print(tag)
[print('-> {} > {}'.format(*attr)) for attr in attrs]
html = '\n'.join([input() for _ in range(int(input()))])
parser = MyHTMLParser()
parser.feed(html)
parser.close()
# Enter your code here. Read input from STDIN. Print output to STDOUT
from HTMLParser import HTMLParser
class parseHTML(HTMLParser):
def handle_starttag(self, tag, attrs):
print tag
for attr in attrs:
print "->", attr[0], ">", attr[1]
parser = parseHTML()
for _ in xrange(input()):
parser.feed(raw_input())
# Enter your code here. Read input from STDIN. Print output to STDOUT
from html.parser import HTMLParser
class MyHTMLParser(HTMLParser):
def handle_starttag(self,tag, attrs):
print(tag)
for i in attrs:
print("->",i[0],">",i[1])
def handle_startendtag(self,tag, attrs):
print(tag)
for i in attrs:
print("->",i[0],">",i[1])
N=int(input())
tmp=""
for _ in range(N):
tmp=tmp+input()+"\n"
psr=MyHTMLParser()
psr.feed(tmp)
In our experience, we suggest you solve this Detect HTML Tags, Attributes and Attribute Values Hacker Rank Solution and gain some new skills from Professionals completely free and we assure you will be worth it.
Detect HTML Tags, Attributes and Attribute Values is available on Hacker Rank for Free, if you are stuck anywhere between compilation, just visit Queslers to get all Hacker Rank Solution
I hope this Detect HTML Tags, Attributes and Attribute Values Hacker Rank Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Hacker Rank, Leetcode, Codechef, Codeforce Solution.
This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.
Keep Learning!
More Hacker Rank Problem & Solutions >>
Mini-Max Sum Hacker Rank Solution
String Validators Hacker Rank Solution
Text Alignment Hacker Rank solution