Get item from bs4.element.Tag

tag.findChild("a")['href']

You grab the "a" tag, then take the "href" attribute


Use css selecor and get the attribute href

from bs4 import BeautifulSoup

data='''<a class="nav-link match-link-stats" href="/football/matches/match867851_Kalteng_Putra-Arema-online/" title="Stat"><i class="icon-match-link"></i></a>'''

soup= BeautifulSoup(data, 'html.parser')
print(soup.select_one('.match-link-stats')['href'])

Output:

/football/matches/match867851_Kalteng_Putra-Arema-online/

This answer assumes you already have the Tag element as an object. If not, use KunduK's answer.


You can use tag.get('href') or tag['href']:

>>> tag.get('href')
'/football/matches/match867851_Kalteng_Putra-Arema-online/'
>>> tag['href']
'/football/matches/match867851_Kalteng_Putra-Arema-online/'

The difference is that tag.get('href') will return None if the attribute doesn't exist, while tag['href'] will raise a KeyError in that case. That's the same behavior as in a dict.

Full example:

>>> from bs4 import BeautifulSoup
>>> soup = BeautifulSoup('<a class="nav-link match-link-stats" href="/football/matches/match867851_Kalteng_Putra-Arema-online/" title="Stat"><i class="icon-match-link"></i></a>')
>>> tag = soup.find('a')
>>> type(tag)
<class 'bs4.element.Tag'>
>>> tag.get('href')
'/football/matches/match867851_Kalteng_Putra-Arema-online/'
>>> tag['href']
'/football/matches/match867851_Kalteng_Putra-Arema-online/'