I need to get value "Anti-Mage" from class in python. How can I do it?
<td ><a href="/players/432283612/matches?hero=anti-mage">Anti-Mage</a><div ><a href="/matches/6107031786"><time data-time-ago="2021-07-26T23:27:54 00:00" datetime="2021-07-26T23:27:54 00:00" title="Mon, 26 Jul 2021 23:27:54 0000">2021-07-26</time></a></div></td>
CodePudding user response:
First, you'll need to select the parent item (td
in this case) from its class name. You can do something like
td = soup.find('td', {'class': 'cell-xlarge'})
and then find the a
children tags with something like this
a = td.findChildren('a', recursive=False)[0]
And this will give you the a
tag. To get its value, you use .string
like this
a_value = a.string
And that gives you the value of Anti-Mage
CodePudding user response:
Assuming HTML posted in your question is the BeautifulSoup
object, call text
method on the <a>
:
soup.a.text
or select more specific with class you mentioned:
soup.select_one('.cell-xlarge a').text
Note: Selecting elements by class
in some cases is only third best option, cause classes can be dynamic, are not unique, ... - Better strategies are to select by id, tag