We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • HackerRank Home

    HackerRank

  • |
  • Prepare
  • Certify
  • Compete
  • Apply
  • Hiring developers?
  1. Prepare
  2. Python
  3. Regex and Parsing
  4. HTML Parser - Part 1
  5. Discussions

HTML Parser - Part 1

Problem
Submissions
Leaderboard
Discussions
Editorial

Sort 153 Discussions, By:

recency

Please Login in order to post a comment

  • thainguyen2893
    1 day ago+ 0 comments
    from html.parser import HTMLParser
    s = ''''''
    for _ in range(int(input())):
        s = s + input() + '\n'
    
    class MyHTMLParser(HTMLParser):
        def handle_starttag(self, tag, attrs):
            print("Start :", tag)
            if attrs:
                for att in attrs:
                    print(f'-> {att[0]} > {att[1]}')
        def handle_endtag(self, tag):
            print("End   :", tag)
        def handle_startendtag(self, tag, attrs):
            print("Empty :", tag)
            if attrs:
                for att in attrs:
                    print(f'-> {att[0]} > {att[1]}')
            
    parser = MyHTMLParser()
    parser.feed(s)
    
    0|
    Permalink
  • info_williamli
    1 week ago+ 0 comments
    from html.parser import HTMLParser
    
    class MyHTMLParser(HTMLParser):
        def handle_starttag(self, tag, attrs):
            print("Start :", tag)
            [print("->", att[0], ">", att[1]) for att in attrs if attrs]
        
        def handle_endtag(self, tag):
            print("End   :", tag)
        
        def handle_startendtag(self, tag, attrs):
            print("Empty :", tag)
            [print("->", att[0], ">", att[1]) for att in attrs if attrs]
    
    parser = MyHTMLParser()
    [parser.feed(input()) for _ in range(int(input()))]
    
    0|
    Permalink
  • ashley_yue117
    2 weeks ago+ 0 comments
    # Enter your code here. Read input from STDIN. Print output to STDOUT
    from html.parser import HTMLParser
    
    class MyHTMLParser(HTMLParser):
        def handle_starttag(self, tag, attrs):
            print("Start :", tag)
            for att in attrs:
                print("-> "+att[0]+" > "+(att[1] if att[1] else "None"))
                
        def handle_endtag(self, tag):
            print("End   :", tag)
            
        def handle_startendtag(self, tag, attrs):
            print("Empty :", tag)
            for att in attrs:
                print("-> "+att[0]+" > "+(att[1] if att[1] else "None"))
            
    parser = MyHTMLParser()
    for _ in range(int(input())):
        parser.feed(input())
    
    0|
    Permalink
  • whidalgohp
    3 weeks ago+ 0 comments
    from html.parser import HTMLParser
    S=''
    for _ in range(int(input())):
        S+=input()
    
    class MyHTMLParser(HTMLParser):
        def handle_starttag(self, tag, attrs):
            print(f'Start : {tag}')
            
            if len(attrs):
                for attr in attrs:
                    print(f'-> {attr[0]} > {attr[1]}')
        def handle_endtag(self, tag):
            print(f'End   : {tag}')
        def handle_startendtag(self, tag, attrs):
            print(f'Empty : {tag}')
            if len(attrs):
                for attr in attrs:
                    print(f'-> {attr[0]} > {attr[1]}')
    
    parser = MyHTMLParser()
    parser.feed(S)
    
    0|
    Permalink
  • dmitriyvgladkov
    2 months ago+ 0 comments
    from html.parser import HTMLParser
    
    self_closing = ['area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', 'keygen', 'link', 'menuitem', 'meta', 'param', 'source', 'track', 'wbr']
    
    class MyHTMLParser(HTMLParser):
        def handle_starttag(self, tag, attrs):
            print("Empty :" if tag in self_closing else "Start :", tag)
            for attr in attrs:
                self.handle_data(attr)
    
        def handle_endtag(self, tag):
            if not (tag in self_closing):
                print("End   :", tag)
    
        def handle_data(self, data):
            if tuple == type(data):
                print('->', data[0], '>', data[1])
    
    parser = MyHTMLParser()
    
    N = int(input())
    
    for line in range(N):
        data = input()
        parser.feed(data)
    
    0|
    Permalink
Load more conversations

Need Help?


View editorial
View top submissions
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy