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 1

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 2

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