HackerRank HTML Parser – Part 2 solution in Python YASH PAL, 31 July 202417 January 2026 HackerRank HTML Parser – Part 2 solution in Python – In this HackerRank HTML Parser – Part 2 problem in Python programming 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.Print the result in the following format: >>> Single-line Comment Comment >>> Data My Data >>> Multi-line Comment Comment_multiline[0] Comment_multiline[1] >>> Data My Data >>> Single-line Comment: Note: Do not print data if data == '\n'..handle_comment(data)This method is called when a comment is encountered (e.g. <!–comment–>).The data argument is the content inside the comment tag:from html.parser import HTMLParserr class MyHTMLParser(HTMLParser): def handle_comment(self, data): print("Comment :", data) .handle_data(data)This method is called to process arbitrary data (e.g. text nodes and the content of <script>…</script> and <style>…</style>).The data argument is the text content of HTML. from html.parser import HTMLParserr class MyHTMLParser(HTMLParser): def handle_data(self, data): print("Data :", data) Input FormatThe first line contains integer N, the number of lines in the HTML code snippet.The next N lines contain HTML code.HackerRank HTML Parse problem solution in Python 2.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()HTML Parser – part 2 solution in Python 3.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 Hackerrank Problems Solutions Python Solutions HackerRankPython