Hello coders, In this post, you will learn how to solve Detect HTML Tags Attributes and Attribute Values in Python HackerRank Solution. This problem is a part of the Python Hacker Rank series.

Detect HTML Tags Attributes and Attribute Values in Python HackerRank Solution
problem
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.
Input Format :
The first line contains an integer N, the number of lines in the HTML code snippet.
The next N lines contain HTML code.
Constraints :
- 0 < N < 100
Output Format :
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.
Sample Input :
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>
Sample Output :
head title object -> type > application/x-flash -> data > your-file.swf -> width > 0 -> height > 0 param -> name > quality -> value > high
Explanation :
- head tag: Print the head tag only because it has no attribute.
- title tag: Print the title tag only because it has no attribute.
- object tag: Print the object tag. In the next 4 lines, print the attributes type, data, width and height along with their respective values.
- <!– Comment –> tag: Don’t detect anything inside it.
- param tag: Print the param tag. In the next 2 Lines, print the attributes name along with their respective values.
Detect HTML Tags Attributes and Attribute Values in Python HackerRank Solution
from html.parser import HTMLParser class MyHTMLParser(HTMLParser): def handle_starttag(self, tag, attrs): print(tag) for attr, value in attrs: print("->", attr, ">", value) def handle_startendtag(self, tag, attrs): print(tag) for attr, value in attrs: print("->", attr, ">", value) html = '' for _ in range(int(input())): html += input().rstrip() + '\n' parser = MyHTMLParser() parser.feed(html) parser.close()
Disclaimer: The above Problem (Detect HTML Tags, Attributes and Attribute Values in Python – HackerRank Solution) is generated by Hackerrank but the Solution is Provided by Chase2Learn. This tutorial is only for Educational and Learning purposes. Authority if any of the queries regarding this post or website fill the following contact form thank you.
FAQ:
Does HackerRank have Python?
HackerRank’s programming challenges can be solved in a variety of programming languages (including Java, C++, PHP, Python, SQL, JavaScript) and span multiple computer science domains.
Where can I practice Python coding?
- HackerRank is a great site for practice that’s also interactive.
Where can I find HackerRank solutions in Python?
in this post, you will get all the solutions to HackerRank Python Problems.
Is possible HackerRank Solution in Python?
HackerRank’s programming challenges can be solved in a variety of programming languages (including Java, C++, PHP, Python, SQL, and JavaScript) and span multiple computer science domains. When a programmer submits a solution to a programming challenge, their submission is scored on the accuracy of their output.
Finally, we are now, in the end, I just want to conclude some important message for you
Note:- I compile all programs, if there is any case program is not working and showing an error please let me know in the comment section. If you are using adblocker, please disable adblocker because some functions of the site may not work correctly.
Please share our posts on social media platforms and also suggest to your friends to Join Our Groups. Don’t forget to subscribe.