Input Format
The first line contains integer , the number of lines in a HTML code snippet.
The next  lines contain HTML code.
Constraints
Output Format
Print the HTML tags, attributes and attribute values in order of their occurrence from top to bottom in the given snippet.
Use proper formatting as explained in the problem statement.

hackerrank html parser part 1 problem solution in python programming

Problem solution in Python 3 programming language.

from HTMLParser import HTMLParser

class MyHTMLParser(HTMLParser):
    def handle_starttag(self, tag, attrs):
        print('Start :',tag)
        for attr in attrs:
                print('->',' > '.join(map(str,attr)))
    def handle_endtag(self, tag):
        print('End   :',tag)
    def handle_startendtag(self, tag, attrs):
        print('Empty :',tag)
        for attr in attrs:
                print('->',' > '.join(map(str,attr)))

html = ""
for i in range(int(input())):
    html += input()
                    
                
parser = MyHTMLParser()
parser.feed(html)
parser.close()

Second solution

# 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 ('Start :',tag)
        for ele in attrs:
            print ('->',ele[0],'>',ele[1])

    def handle_endtag(self, tag):
        print ('End   :',tag)
    def handle_startendtag(self, tag, attrs):
        print ('Empty :',tag)
        for ele in attrs:
            print ('->',ele[0],'>',ele[1])
MyParser = MyHTMLParser()
MyParser.feed(''.join([input().strip() for _ in range(int(input()))]))

Problem solution in Python 2 programming language.

from HTMLParser import HTMLParser

class MyHTMLParser(HTMLParser):
    def handle_starttag(self, tag, attrs):
        print 'Start :',tag
        for attr in attrs:
                print '->',' > '.join(map(str,attr))
    def handle_endtag(self, tag):
        print 'End   :',tag
    def handle_startendtag(self, tag, attrs):
        print 'Empty :',tag
        for attr in attrs:
                print '->',' > '.join(map(str,attr))

html = ""
for i in range(int(raw_input())):
    html += raw_input()
                    
                
parser = MyHTMLParser()
parser.feed(html)
parser.close()