Skip to content
Programming101
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programmingoneonone

Learn everything about programming

HackerRank HTML Parser – Part 2 solution in python

YASH PAL, 31 July 2024

In this HTML Parser- Part 2 problem You are given an HTML code snippet of N lines. Your task is to print the single-line comments, multi-line comments, and the data.

HackerRank HTML Parser - Part 2 solution in python

Topics we are covering

Toggle
  • Problem solution in Python 2 programming.
  • Problem solution in Python 3 programming.
    • Problem solution in pypy programming.
    • Problem solution in pypy3 programming.

Problem solution in Python 2 programming.

from HTMLParser import HTMLParser

class MyHTMLParser(HTMLParser):
    def handle_comment(self, data):
        if "n" in data:
            print ">>> Multi-line Comment"
        else:
            print ">>> Single-line Comment"
        print data
    def handle_data(self, data):
        if len(data.strip())>0:
            print ">>> Data"
            print data

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

Problem solution in Python 3 programming.

from html.parser import HTMLParser
html = ""
class MyHTMLParser(HTMLParser):
    def handle_comment(self,data):
        if('n' in data):
            print(">>> Multi-line Comment")
        else:
            print(">>> Single-line Comment")
        print(data)
    def handle_data(self,data):
        if(data != 'n'):
            print(">>> Data")
            print(data)
             
for i in range(int(input())):
    html += input().rstrip()
    html += 'n'
#print(html)    
parser = MyHTMLParser()
parser.feed(html)
parser.close()

Problem solution in pypy programming.

from HTMLParser import HTMLParser
class CustomHTMLParser(HTMLParser):
    def handle_comment(self, data):
        number_of_line = len(data.split('n'))
        if number_of_line>1:
            print('>>> Multi-line Comment')
        else:
            print('>>> Single-line Comment')
        if data.strip():
            print(data)

    def handle_data(self, data):
        if data.strip():
            print(">>> Data")
            print(data)

parser = CustomHTMLParser()

n = int(input())

html_string = ''
for i in range(n):
    html_string += raw_input().rstrip()+'n'
    
parser.feed(html_string)
parser.close()

Problem solution in pypy3 programming.

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

class MyHTMLParser(HTMLParser):
    def handle_comment(self, data):
        lines = data.split("r")
        if len(lines) > 1:
            print(">>> Multi-line Comment")
            for l in lines:
                print(l)
        else:
            print(">>> Single-line Comment")
            print(lines[0])

    def handle_data(self, data):
        if len(data.strip()) == 0:
            return
        print(">>> Data")
        lines = data.split("r")
        for l in lines:
            if l.strip() != "":
                print(l)

n = int(input())
input_data =""
for _ in range(n):
    input_data += input()
parser = MyHTMLParser()
parser.feed(input_data)

coding problems solutions Python Solutions

Post navigation

Previous post
Next post
  • Automating Image Format Conversion with Python: A Complete Guide
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes