Beautiful Soup 简明教程

Beautiful Soup - find_next_sibling() Method

Method Description

Beautiful Soup 中的 find_next_sibling() 方法查找与给定条件匹配的,且在文档中之后出现的,在这个 PageElement 中的同级中最接近的同级。此方法类似于 next_sibling 属性。

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

  1. name − A filter on tag name.

  2. attrs − A dictionary of filters on attribute values.

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

  4. kwargs − A dictionary of filters on attribute values.

Return Type

find_next_sibling() 方法返回 Tag 对象或 NavigableString 对象。

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

如果下一个节点不存在,该方法返回 None。

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