Beautiful Soup 简明教程

Beautiful Soup - find_previous() Method

Method Description

Beautiful Soup 中的 find_previous() 方法从该 PageElement 文档中向后查找并查找与给定条件匹配的第一个 PageElement。它返回在文档中当前标签之前的第一个标签或 NavigableString。与所有其他查找方法一样,该方法具有以下语法 -

The find_previous() method in Beautiful Soup look backwards in the document from this PageElement and find the first PageElement that matches the given criteria. It returns the first tag or NavigableString that comes before the current tag in the document. Like all other find methods, this method has the following syntax −

Syntax

find_previous(name, attrs, string, **kwargs)

Parameters

  1. name − A filter on tag name.

  2. attrs − A dictionary of filters on attribute values.

  3. string − A filter for a NavigableString with specific text.

  4. kwargs − A dictionary of filters on attribute values.

Return Value

find_previous() 方法返回 Tag 或 NavigableString 对象。

The find_previous() method returns a Tag or NavigableString object.

Example 1

在下面的示例中,我们尝试查找 <body> 标签之前的对象。碰巧是 <title> 元素。

In the example below, we try to find which is the previous object before the <body> tag. It happens to be <title> element.

from bs4 import BeautifulSoup

fp = open("index.html")
soup = BeautifulSoup(fp, 'html.parser')
tag = soup.body
print (tag.find_previous())

Output

<title>TutorialsPoint</title>

Example 2

在该示例中使用的 HTML 文档中有三个输入元素。下面的代码找到 name 属性 = age 的输入元素并查找其前面的元素。

There are three input elements in the HTML document used in this example. The following code locates the input element with name attribute = age and looks for its previous element.

from bs4 import BeautifulSoup

fp = open("index.html")
soup = BeautifulSoup(fp, 'html.parser')
tag = soup.find('input', {'name':'age'})
print (tag.find_previous())

Output

<input id="nm" name="name" type="text"/>

Example 3

<title> 之前的元素碰巧是 <head> 元素。

The element before <title> happens to be <head> element.

from bs4 import BeautifulSoup

fp = open("index.html")
soup = BeautifulSoup(fp, 'html.parser')
tag = soup.find('title')
print (tag.find_previous())

Output

<head>
<title>TutorialsPoint</title>
</head>