Beautiful Soup | next_siblings property
Start your free 7-days trial now!
In Beautiful Soup, the next_siblings
property of a tag or a string returns a generator used to iterate over all the subsequent tags and strings under the same parent.
Examples
Consider the following HTML document:
my_html = """ <div> <p>Alex</p> <p>Bob</p> </div>"""soup = BeautifulSoup(my_html)
Let's get the next_siblings
of <p>Alex</p>
:
Here, we've used the repr(~)
method to escape the newline character so that you can explicitly see '\n'
instead of an empty line. To break this down, the next_sibling
of <p>Alex</p>
is a newline character, which might be surprising for those who expected to see <p>Bob</p>
. Such a result arises because there is a new line character \n
between <p>Alex</p>
and <p>Bob</p>
.
If you just wanted to access the next sibling elements, then the better alternative would be to call the find_next_siblings()
method:
<p>Bob</p><p>Cathy</p>