I'm playing with BeautifulSoup 4 and I have this html code:
</tr>
<tr>
<td id="freistoesse">Giraffe</td>
<td>14</td>
<td>7</td>
</tr>
I want to match both values between <td>
tags so here 14 and 7.
I tried this:
giraffe = soup.find(text='Giraffe').findNext('td').text
but this only matches 14
. How can I match both values with this function?
Use
find_all
instead offindNext
:yields
Or, you could use
find_next_siblings
(also known asfetchNextSiblings
):yields
Explanation:
Note that
soup.find(text='Giraffe')
returns a NavigableString.To get the associated
td
tag, useor
Once you have the
td
tag, you could usefind_next_siblings
:PS. BeautifulSoup has added method names that use underscores instead of CamelCase. They do the same thing, but comform to the PEP8 style guide recommendations. Thus, prefer
find_next_siblings
overfetchNextSiblings
.