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"/>
广告

© . All rights reserved.