from html.parser import HTMLParser class DivTracker(HTMLParser): def __init__(self): super().__init__() self.stack = [] self.found = False def handle_starttag(self, tag, attrs): if tag == 'div': attrs_dict = dict(attrs) if attrs_dict.get('id') == 'tab-backtest': self.found = True if self.found: # store class and id for debugging cls = attrs_dict.get('class', '') id_ = attrs_dict.get('id', '') self.stack.append((self.getpos(), cls, id_)) def handle_endtag(self, tag): if tag == 'div' and self.found: if self.stack: self.stack.pop() if len(self.stack) == 0: self.found = False # End of tab-backtest with open('templates/backtest.html', 'r', encoding='utf-8') as f: html = f.read() tracker = DivTracker() tracker.feed(html) print("Unclosed divs inside tab-backtest:") for item in tracker.stack: print(item)