Beautiful Soup - find_all_next() 方法



方法描述

Beautiful Soup 中的 find_all_next() 方法查詢與給定條件匹配且出現在文件中此元素之後的所有 PageElements。此方法返回標籤或 NavigableString 物件,並且該方法接受與 find_all() 完全相同的引數。

語法

find_all_next(name, attrs, string, limit, **kwargs)

引數

  • name - 標籤名稱的過濾器。

  • attrs - 屬性值的過濾器字典。

  • recursive - 如果為 True,則執行遞迴搜尋。否則,僅考慮直接子元素。

  • limit - 找到指定數量的匹配項後停止查詢。

  • kwargs - 屬性值的過濾器字典。

返回值

此方法返回一個包含 PageElements(標籤或 NavigableString 物件)的 ResultSet。

示例 1

使用 index.html 作為此示例的 HTML 文件,我們首先找到 <form> 標籤並使用 find_all_next() 方法收集其後的所有元素。

from bs4 import BeautifulSoup

fp = open("index.html")
soup = BeautifulSoup(fp, 'html.parser')

tag = soup.form
tags = tag.find_all_next()
print (tags)

輸出

[<input id="nm" name="name" type="text"/>, <input id="age" name="age" type="text"/>, <input id="marks" name="marks" type="text"/>]

示例 2

在這裡,我們將過濾器應用於 find_all_next() 方法,以收集 <form> 之後的所有標籤,其 id 為 nm 或 age。

from bs4 import BeautifulSoup

fp = open("index.html")
soup = BeautifulSoup(fp, 'html.parser')

tag = soup.form
tags = tag.find_all_next(id=['nm', 'age'])
print (tags)

輸出

[<input id="nm" name="name" type="text"/>, <input id="age" name="age" type="text"/>]

示例 3

如果我們檢查 body 標籤之後的標籤,它包括一個 <h1> 標籤以及 <form> 標籤,其中包含三個輸入元素。

from bs4 import BeautifulSoup

fp = open("index.html")
soup = BeautifulSoup(fp, 'html.parser')

tag = soup.body
tags = tag.find_all_next()
print (tags)

輸出

<h1>TutorialsPoint</h1>
<form>
<input id="nm" name="name" type="text"/>
<input id="age" name="age" type="text"/>
<input id="marks" name="marks" type="text"/>
</form>
<input id="nm" name="name" type="text"/>
<input id="age" name="age" type="text"/>
<input id="marks" name="marks" type="text"/>
廣告