I try to convert an HTML page into a tree structure. I have derived this class (I removed what I actually do with each tag as it's not relevant) :
class PageParser(html.parser.HTMLParser):
def handle_starttag(self, tag, attrs):
print("start "+tag)
def handle_endtag(self, tag):
print("end "+tag)
def handle_startendtag(self, tag, attrs):
print("startend "+tag)
I expected empty elements to trigger the handle_startendtag
method. The problem is that, when encountering an empty element like <meta>
, only the handle_starttag
method is called. The tag is never closed from my class' point of view :
parser = PageParser()
parser.feed('<div> <meta charset="utf-8"> </div>')
prints :
start div
start meta
end div
I need to know when each element has been closed to correctly create the tree. How can I know if a tag is an empty element ?
` or the `` tags. – Krishna Pradyumna Mokshagundam May 09 '17 at 10:27