[Solved] Beautifulsoup: Is it possible to get tag name and attribute name by its value? [closed]


You could define a filter function that checks if there is one HTML tag with a attribute value equal to value:

def your_filter(tag, value):
    for key in tag.attrs.keys():
        if tag[key] == value:
            return True
    return False

# alternatively as one liner:
def your_filter(tag, value):
    return any(tag[key] == value for key in tag.attrs.keys())    

Then, you could use it like this:


soup = BeautifulSoup(html_code)
tags = soup.find_all(lambda tag: your_filter(tag, "icaec13e17ee4432d9971f5e4b3d32ba1_265"))

0

solved Beautifulsoup: Is it possible to get tag name and attribute name by its value? [closed]