SIM
SIM

Reputation: 22440

Trouble dealing with "next_sibling" in BeautifulSoup

I'm trying to understand the practical usage of next_sibling in BeautifulSoup. I've searched a lot but could not find examples that can satisfy my requirement. However, If i try to get the text within the 2nd td tag starting from the first tr using next_sibling. I've tried a way but it gives me error. Hope somebody can help me on this. Thanks.

Elements:

html_content="""
<tr>
    <td>Not this one</td>
</tr>
<tr>
    <td>There it is!!</td>
</tr>
"""

I tried like below but it throws error:

from bs4 import BeautifulSoup
soup = BeautifulSoup(html_content,"lxml")
item = soup.find("tr").next_sibling.find("td").text
print(item)

Once again my intention is to parse There it is!! starting from first tr tag and using next_sibling. Thanks.

Upvotes: 2

Views: 256

Answers (1)

Andersson
Andersson

Reputation: 52665

It seem tha you're searching for find_next_sibling():

item = soup.find("tr").find_next_sibling().find('td').text

Upvotes: 2

Related Questions