from html.parser import HTMLParser
class MyParser(HTMLParser):
def handle_starttag(self, tag, attrs):
print('start <%s>' % tag)
print("attrs", end="")
if attrs:
print(attrs[0][0])
def handle_endtag(self, tag):
print('end </%s>' % tag)
def handle_startendtag(self, tag, attrs):
print('start end <%s/>' % tag)
def handle_data(self, data):
print("data "+data)
def handle_comment(self, data):
print('<!--', data, '-->')
def handle_entityref(self, name):
print('sssss&%s;' % name)
def handle_charref(self, name):
print('eeeee&#%s;' % name)
parser = MyParser()
data = '''<html><head h='abcd'></head>wgwfew</html>'''
parser.feed(data)