from html.parser import HTMLParser class DivTracker(HTMLParser): def __init__(self): super().__init__() self.stack = [] self.found_tab_backtest = False def handle_starttag(self, tag, attrs): if tag == 'div': attrs_dict = dict(attrs) if attrs_dict.get('id') == 'tab-backtest': self.found_tab_backtest = True if self.found_tab_backtest: self.stack.append(self.getpos()) def handle_endtag(self, tag): if tag == 'div' and self.found_tab_backtest: if self.stack: self.stack.pop() with open('templates/backtest.html', 'r', encoding='utf-8') as f: html = f.read() # Only process up to tab-backtest end end_idx = html.find('tab-backtest end') if end_idx != -1: # include the before it end_div_idx = html.rfind('', 0, end_idx) html = html[:end_div_idx+6] tracker = DivTracker() tracker.feed(html) print("Unclosed div start positions inside tab-backtest:") for pos in tracker.stack: print(pos)