Beautiful Soup 简明教程
Beautiful Soup - find_previous_sibling() Method
Method Description
Beautiful Soup中的find_previous_sibling()方法返回最早出现在文档中并且符合给定条件的与该PageElement最接近的兄弟元素。
The find_previous_sibling() method in Beautiful Soup returns the closest sibling to this PageElement that matches the given criteria and appears earlier in the document.
Parameters
-
name − A filter on tag name.
-
attrs − A dictionary of filters on attribute values.
-
string − A filter for a NavigableString with specific text.
-
kwargs − A dictionary of filters on attribute values.
Return Value
find_previous_sibling()方法返回一个可能是Tag或NavigableString的PageElement。
The find_previous_sibling() method returns a PageElement that could be a Tag or a NavigableString.
Example 1
从以下示例中使用的HTML字符串,我们可以找出标签名为“u”的<i>标签的前一兄弟元素。
From the HTML string used in the following example, we find out the previous sibling of <i> tag, having the tag name as 'u'
from bs4 import BeautifulSoup
fp = open("index.html")
soup = BeautifulSoup("<p><u>Excellent</u><b>Hello</b><i>Python</i></p>", 'html.parser')
tag = soup.i
sibling = tag.find_previous_sibling('u')
print (sibling)
Example 2
网页(index.html)有一个HTML表单,其中包含三个输入元素。我们通过id属性找到marks,然后找到其前一个兄弟,该兄弟的id设置为nm。
The web page (index.html) has a HTML form with three input elements. We locate one with id attribute as marks and then find its previous sibling that had id set to nm.
from bs4 import BeautifulSoup
fp = open("index.html")
soup = BeautifulSoup(fp, 'html.parser')
tag = soup.find('input', {'id':'marks'})
sib = tag.find_previous_sibling(id='nm')
print (sib)
Example 3
在下面的代码中,HTML字符串有两个<p>元素和一个位于外部<p>标签內的字符串。我们使用find_previous_string()方法来查找与<p>Tutorial</p>标签处于兄弟关系的NavigableString对象。
In the code below, the HTML string has two <p> elements and a string inside the outer <p> tag. We use find_previous_string() method to search for the NavigableString object sibling of <p>Tutorial</p> tag.
html = '''
<p>Excellent<p>Python</p><p>Tutorial</p></p>
'''
from bs4 import BeautifulSoup
soup = BeautifulSoup(html, 'html.parser')
tag = soup.find('p', string='Tutorial')
ptag = tag.find_previous_sibling(string='Excellent')
print (ptag, type(ptag))