Beautiful Soup - Functions Reference

Beautiful Soup Useful Resources

Beautiful Soup - find_next_sibling() Method



Method Description

The find_next_sibling() method in Beautiful Soup Find the closest sibling at the same level to this PageElement that matches the given criteria and appears later in the document. This method is similar to next_sibling property.

Syntax

find_fnext_sibling(name, attrs, string, **kwargs)

Parameters

  • name − A filter on tag name.

  • attrs − A dictionary of filters on attribute values.

  • string − The string to search for (rather than tag).

  • kwargs − A dictionary of filters on attribute values.

Return Type

The find_next_sibling() method returns Tag object or a NavigableString object.

Example - Usage of find_next_sibling() method

from bs4 import BeautifulSoup

soup = BeautifulSoup("<p><b>Hello</b><i>Python</i></p>", 'html.parser')

tag1 = soup.find('b')
print ("next:",tag1.find_next_sibling())

Output

next: <i>Python</i>

Example - Case of no Next Sibling

If the next node doesn't exist, the method returns None.

from bs4 import BeautifulSoup

soup = BeautifulSoup("<p><b>Hello</b><i>Python</i></p>", 'html.parser')

tag1 = soup.find('i')
print ("next:",tag1.find_next_sibling())

Output

next: None
Advertisements