Beautiful Soup 简明教程

Beautiful Soup - next_element Property

Method Description

在 Beautiful Soup 库中,next_element 属性返回紧邻当前 PageElement 的 Tag 或 NavigableString,即使它位于父树之外。还有一个具有类似行为的 next 属性

In Beautiful Soup library, the next_element property returns the Tag or NavigableString that appears immediately next to the current PageElement, even if it is out of the parent tree. There is also a next property which has similar behaviour

Syntax

Element.next_element

Return value

next_element 和 next 属性返回紧邻当前标记的标记或 NavigableString。

The next_element and next properties return a tag or a NavigableString appearing immediately next to the current tag.

Example 1

在从给定 HTML 字符串解析的文档树中,我们找到 <b> 标记的 next_element

In the document tree parsed from the given HTML string, we find the next_element of the <b> tag

html = '''
<p><b>Excellent</b><p>Python</p><p id='id1'>Tutorial</p></p>
'''
from bs4 import BeautifulSoup

soup = BeautifulSoup(html, 'lxml')
tag = soup.b
print (tag)
nxt = tag.next_element
print ("Next:",nxt)

nxt = tag.next_element.next_element
print ("Next:",nxt)

Output

<b>Excellent</b>
Next: Excellent
Next: <p>Python</p>

输出有点奇怪,因为 <b>Excellent</b> 的下一个元素显示为“Excellent”,这是因为内部字符串被注册为下一个元素。若要将所需结果 (<p>Python</p>) 作为下一个元素,请提取内部 NavigableString 对象的 next_element 属性。

The output is a little strange as the next element for <b>Excellent</b> is shown to be 'Excellent', that is because the inner string is registered as the next element. To obtain the desired result (<p>Python</p>) as the next element, fetch the next_element property of the inner NavigableString object.

Example 2

BeautifulSoup PageElements 还支持类似于 next_element 属性的 next 属性

The BeautifulSoup PageElements also support next property which is analogous to next_element property

html = '''
<p><b>Excellent</b><p>Python</p><p id='id1'>Tutorial</p></p>
'''
from bs4 import BeautifulSoup
soup = BeautifulSoup(html, 'lxml')
tag = soup.b
print (tag)
nxt = tag.next
print ("Next:",nxt)

nxt = tag.next.next
print ("Next:",nxt)

Output

<b>Excellent</b>
Next: Excellent
Next: <p>Python</p>

Example 3

在下一个示例中,我们尝试确定 <body> 标记旁边的元素。因为它后面跟着一个换行符 (\n),我们需要找到 body 标记旁边的一个元素的下一个元素。碰巧是 <h1> 标记。

In the next example, we try to determine the element next to <body> tag. As it is followed by a line break (\n), we need to find the next element of the one next to body tag. It happens to be <h1> tag.

from bs4 import BeautifulSoup

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

tag = soup.find('body')
nxt = tag.next_element.next
print ("Next:",nxt)

Output

Next: <h1>TutorialsPoint</h1>