I have element with type bs4.element.Tag
<a class="nav-link match-link-stats" href="/football/matches/match867851_Kalteng_Putra-Arema-online/" title="Stat"><i class="icon-match-link"></i></a>
And I want to get "/football/matches/match867851_Kalteng_Putra-Arema-online/" from this element. How to do it?
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/'
tag.findChild("a")['href']
You grab the "a" tag, then take the "href" attribute
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With