Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get item from bs4.element.Tag

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?

like image 336
Ильшат Мурзурбеков Avatar asked Aug 07 '19 13:08

Ильшат Мурзурбеков


2 Answers

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/'
like image 139
Brad Solomon Avatar answered Oct 10 '22 16:10

Brad Solomon


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

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

like image 42
Ron Serruya Avatar answered Oct 10 '22 15:10

Ron Serruya