一、方法描述
在 BeautifulSoup 库中,next_elements
属性返回一个生成器对象,包含解析树中当前元素之后的所有字符串或标签。
二、语法
Element.next_elements
三、返回值
next_elements
属性返回一个生成器。
四、示例
示例 1
next_elements
属性返回在下面的文档字符串中 <b>
标签之后出现的标签和 NavigableStrings。
html = '''
<p><b>Excellent</b><p>Python</p><p id='id1'>Tutorial</p></p>
'''
from bs4 import BeautifulSoup
soup = BeautifulSoup(html, 'html.parser')
tag = soup.find('b')
nexts = tag.next_elements
print("Next elements:")
for next in nexts:
print(next)
输出:
Next elements:
Excellent
Python
Python
<p id="id1">Tutorial</p>
Tutorial
示例 2
下面列出了所有出现在 <p>
标签之后的元素。
from bs4 import BeautifulSoup
html = '''
<p>
<b>Excellent</b><i>Python</i>
</p>
<u>Tutorial</u>
'''
soup = BeautifulSoup(html, 'html.parser')
tag1 = soup.find('p')
print("Next elements:")
print(list(tag1.next_elements))
输出:
Next elements:
['\n', <b>Excellent</b>, 'Excellent', <i>Python</i>, 'Python', '\n', '\n', <u>Tutorial</u>, 'Tutorial', '\n']
示例 3
下面列出了 index.html
文件中 HTML 表单里 <input>
标签之后的元素。
from bs4 import BeautifulSoup
fp = open("index.html")
soup = BeautifulSoup(fp, 'html5lib')
tag = soup.find('input')
nexts = tag.next_elements
print("Next elements:")
for next in nexts:
print(next)
输出:
Next elements:
<input id="age" name="age" type="text"/>
<input id="marks" name="marks" type="text"/>
请注意,在示例 3 中的代码中,原本使用的是 soup.previous_elements
,这应该是 tag.next_elements
的笔误。根据上下文,应该是在查找 <input>
标签之后的元素,而不是之前的元素。因此,在代码示例中已经更正为正确的 tag.next_elements
。