How to select tags by attribute value with Beautiful Soup

You can simply do this :

soup = BeautifulSoup(html)
results = soup.findAll("a", {"data-name" : "result-name"})

Source : How to find tags with only certain attributes - BeautifulSoup


html = """
<div class="headercolumn">
<h2>
<a class="results" data-name="result-name" href="/xxy> my text</a>
</h2>
"""

from bs4 import BeautifulSoup
soup = BeautifulSoup(html)
for d in soup.findAll("div",{"class":"headercolumn"}):
    print d.a.get("data-name")
    print d.select("a.results")

result-name
[<a class="results" data-name="result-name" href="/xxy&gt; my text&lt;/a&gt;&lt;/h2&gt;"></a>]

select classes or ids

soup.select('a.gamers') # select an `a` tag with the class gamers
soup.select('a#gamer') # select an `a` tag with the id gamer

select single attr:

soup.select('a[attr="value"]')

select multiple attr:

attr_dict = {
             'attr1': 'val1',
             'attr2': 'val2',
             'attr3': 'val3'
            }

soup.findAll('a', attr_dict)

you can use any CSS selector in soup.select