When scraping web pages, it's common to extract an element, and then want to get the next element following it in the HTML. BeautifulSoup provides an easy way to do this using the .next_sibling attribute.
What is next_sibling?
The
For example:
<div>
<p>First paragraph</p>
<p>Second paragraph</p>
</div>
first_p = soup.find('p')
print(first_p.next_sibling)
# <p>Second paragraph</p>
It returns the next
Advantages of next_sibling
The main advantages of using
Example Usage
Some common examples where
Get text after a heading:
h2 = soup.find('h2')
print(h2.next_sibling.text)
Loop through table rows:
row = soup.find('tr')
while row:
print(row.text)
row = row.next_sibling
Extract field labels and values:
label = soup.find('label')
value = label.next_sibling.text
Handling No Sibling
If there is no next sibling,
last_p = soup.find('p', class='last')
print(last_p.next_sibling) # None
So check for
In summary,