Detect HTML Tags, Attributes and Attribute Values

  • + 1 comment

    I want to share my solution with BeautifulSoup 4 (failed):

    from bs4 import BeautifulSoup as bs
    n = int(input())
    page = ''
    for i in range(n):
        page = page + "\n" +input()
    soup = bs(page)
    for tag in soup.find_all(True):
        print(tag.name)
        for at in tag.attrs.keys():
            print("-> "+at+" > "+tag[at])
    

    It does output the right elements, but attributes ordered alphabetically, not the same order as the original file. So it doesn't pass the expected output test. Any opinion?. Thanks!