Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find parent tags of an element with BeautifulSoup?

I want to know if its possible to me get, using beautiful soup, a few lines up of html line:

<tr id="12590559" class="">
<td>
<span class="he16-1 tip-top" title="Cracker"></span>
</td>
<td>
cracker.crc
</td>

In that example I want to extract the id but with the tittle information using:

soup = BeautifulSoup(lista.content, "lxml")
id = soup.find(attrs={"title": "Cracker"})

I can get the

<span class="he16-1 tip-top" title="Cracker"></span>

but I want to get also the id. Can I use the BeautifulSoup to get a few lines up?

like image 842
Mateus Oliveira Avatar asked Jun 29 '18 18:06

Mateus Oliveira


1 Answers

Use BeautifulSoup's find_parent /find_parents method.

pass tr as parent search item and ['id'] will print id value

id.find_parent('tr')['id']

>> '12590559'
like image 117
Morse Avatar answered Oct 17 '22 12:10

Morse