HackerRank Python - HTML Parser - Part 2




.handle_comment(data)
This method is called when a comment is encountered (e.g. ).
The data argument is the content inside the comment tag:

.handle_data(data)
This method is called to process arbitrary data (e.g. text nodes and the content of and ).
The data argument is the text content of HTML.



  1. from html.parser import HTMLParser
  2. class MyHTMLParser(HTMLParser):
  3. def handle_comment(self, comment):
  4. if '\n' in comment:
  5. print('>>> Multi-line Comment')
  6. else:
  7. print('>>> Single-line Comment')
  8. print(comment)
  9. def handle_data(self, data):
  10. if data == '\n': return
  11. print('>>> Data')
  12. print(data)
  13. html = ""
  14. for i in range(int(input())):
  15. html += input().rstrip()
  16. html += '\n'
  17. parser = MyHTMLParser()
  18. parser.feed(html)
  19. parser.close()




codesadda.com

Codesadda.com is your home of programming solutions, tutorials, video tutorials and much more. Sign Up for our weekly newsletter to get update about new content.

Like us on Facebook | Connect with us on LinkedIn | Subscribe our Channel on Youtube