Header Ad

HackerRank HTML Parser - Part 1 solution in python

In this HTML Parser - part 1 problem You are given an HTML code snippet of N lines. Your task is to print start tags, end tags, and empty tags separately.

HackerRank HTML Parser - Part 1 solution in python


Problem solution in Python 2 programming.

from HTMLParser import HTMLParser

# create a subclass and override the handler methods
class MyHTMLParser(HTMLParser):
    def handle_starttag(self, tag, attrs):
        print "Start :", tag
        for attr in attrs:
            print "->", attr[0], ">", attr[1]
    def handle_endtag(self, tag):
        print "End   :", tag
    def handle_startendtag(self, tag, attrs):
        print "Empty :", tag
        for attr in attrs:
            print "->", attr[0], ">", attr[1]

N = int(input())
parser = MyHTMLParser()

for i in xrange(N):
    parser.feed(raw_input())




Problem solution in Python 3 programming.

# 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 pypy programming.

from HTMLParser import HTMLParser

class MyHTMLParser(HTMLParser):
    def handle_starttag(self, tag, attrs):
        if tag == 'comment':
            return None
        print('Start : {0}'.format(tag))
        if attrs:
            for el in attrs:
                print('-> {0} > {1}'.format(el[0], el[1]))
    def handle_endtag(self, tag):
        if tag == 'comment':
            return None
        print('End   : {0}'.format(tag))
    def handle_startendtag(self, tag, attrs):
        if tag == 'comment':
            return None
        print('Empty : {0}'.format(tag))
        if attrs:
            for el in attrs:
                print('-> {0} > {1}'.format(el[0], el[1]))
        
h = MyHTMLParser()
single_html_str = ''
for _ in range(input()):
    single_html_str += raw_input()
h.feed(single_html_str)


Problem solution in pypy3 programming.

# Enter your code here. Read input from STDIN. Print output to STDOUT
import re
from html.parser import HTMLParser

class MyHTMLParser(HTMLParser):
    def handle_starttag(self, tag, attrs):
        print ("Start :", tag)
        self.value(attrs)

    def handle_endtag(self, tag):
        print ("End   :", tag)

    def handle_startendtag(self, tag, attrs):
        print ("Empty :", tag)
        self.value(attrs)

    def value(self, attrs = None):
        if attrs:
            [print('->', attr, '>', val) for attr, val, in attrs]

ss = '\n'.join([input() for x in range(int(input()))])
parser = MyHTMLParser()
parser.feed(ss)


Post a Comment

0 Comments