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
廣告