Detect HTML Tags, Attributes and Attribute Values

  • + 1 comment
    from html.parser import HTMLParser
    
    class MyHTMLParser(HTMLParser):
        def handle_starttag(self, tag, attrs):
            print(tag)
            if attrs:
                [print(f'-> {attr[0]} > {attr[1]}') for attr in attrs]
        def handle_startendtag(self, tag, attrs):
            print(tag)
            if attrs:
                [print(f'-> {attr[0]} > {attr[1]}') for attr in attrs]  
      
    html = ""       
    for _ in range(int(input())):
        html += input().rstrip()
        html += '\n'
        
    parser = MyHTMLParser()
    parser.feed(html)
    parser.close()