Beautiful Soup 简明教程
Beautiful Soup - previous_sibling Property
Method Description
在同一缩进级别上出现的 HTML 标签称为同级标签。PageElement 的 previous_sibling 属性返回一个前一个标签(出现在当前标签之前的标签),该标签处于同一级别或位于相同父项下。此属性封装了 find_previous_sibling() 方法。
The HTML tags appearing at the same indentation level are called siblings. The previous_sibling property of the PageElement returns a previous tag (a tag appearing before the current tag) at the same level, or under the same parent. This property encapsulates the find_previous_sibling() method.
Return type
previous_sibling 属性返回 PageElement、Tag 或 NavigableString 对象。
The previous_sibling property returns a PageElement, a Tag or a NavigableString object.
Example 1
在以下代码中,HTML 字符串包含在 <p> 标记中的两个相邻标记。它显示在 <b> 标记之前出现的兄弟标记。
In the following code, the HTML string consists of two adjacent tags inside a <p> tag. It shows the sibling tag for <b> tag appearing before it.
from bs4 import BeautifulSoup
fp = open("index.html")
soup = BeautifulSoup("<p><b>Hello</b><i>Python</i></p>", 'html.parser')
tag = soup.i
sibling = tag.previous_sibling
print (sibling)
Example 2
我们使用 index.html 文件进行解析。该页面包含带有三个输入元素的 HTML 表单。哪个元素是具有 id 属性为 age 的输入元素的前一兄弟元素?以下代码显示了它:
We are using the index.html file for parsing. The page contains a HTML form with three input elements. Which element is a previous sibling of input element with its id attribute as age? The following code shows it −
from bs4 import BeautifulSoup
fp = open("index.html")
soup = BeautifulSoup(fp, 'html.parser')
tag = soup.find('input', {'id':'age'})
sib = tag.previous_sibling.previous_sibling
print (sib)
Example 3
首先我们找到包含字符串“教程”的 <p> 标签,然后找到它的前一个标签。
First we find the <p> tag containing the string 'Tutorial' and then fins a tag previous to it.
html = '''
<p>Excellent</p><p>Python</p><p>Tutorial</p>
'''
from bs4 import BeautifulSoup
soup = BeautifulSoup(html, 'html.parser')
tag = soup.find('p', string='Tutorial')
print (tag.previous_sibling)