Beautiful Soup - find_next_sibling() 方法



方法描述

Beautiful Soup 中的 find_next_sibling() 方法查找与给定条件匹配且在文档中后面出现的与该 PageElement 位于同一级别的最接近的同级元素。此方法类似于 next_sibling 属性。

语法

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

参数

  • name − 标签名称过滤器。

  • attrs − 属性值过滤器字典。

  • string − 要搜索的字符串(而不是标签)。

  • kwargs − 属性值过滤器字典。

返回类型

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

示例 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())

输出

next: <i>Python</i>

示例 2

如果下一个节点不存在,则方法返回 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())

输出

next: None
广告
© . All rights reserved.