You are viewing a single comment's thread. Return to all comments →
from html.parser import HTMLParser
class MyHTMLParser(HTMLParser): def handle_starttag(self, tag, attrs): print("Start :", tag) for attr in attrs: print("->", attr[0], ">", attr[1] if attr[1] else "None")
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] if attr[1] else "None")
n = int(input().strip()) html_code = '\n'.join(input() for _ in range(n))
parser = MyHTMLParser() parser.feed(html_code)
Seems like cookies are disabled on this browser, please enable them to open this website
HTML Parser - Part 1
You are viewing a single comment's thread. Return to all comments →
from html.parser import HTMLParser
class MyHTMLParser(HTMLParser): def handle_starttag(self, tag, attrs): print("Start :", tag) for attr in attrs: print("->", attr[0], ">", attr[1] if attr[1] else "None")
Read input
n = int(input().strip()) html_code = '\n'.join(input() for _ in range(n))
Create parser and feed the HTML code
parser = MyHTMLParser() parser.feed(html_code)