Beautiful Soup 简明教程

Beautiful Soup - find_all_previous() Method

Method Description

Beautiful Soup 中的 find_all_previous() 方法会向后查看此 PageElement 文档,并查找与给定条件匹配且出现在当前元素之前的所有 PageElements。它返回一个 ResultsSet 的 PageElements,该结果集出现在文档中的当前标记之前。与所有其他查找方法一样,此方法具有以下语法:

Syntax

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

Parameters

  1. name − 对标记名称的筛选。

  2. attrs − 对属性值进行筛选的字典。

  3. string - 具有特定文本的 NavigableString 的过滤器。

  4. limit − 找到大量结果后停止查找。

  5. kwargs − 对属性值进行筛选的字典。

Return Value

find_all_previous() 方法返回一个 Tag 或 NavigableString 对象的结果集。如果 limit 参数为 1,则该方法等效于 find_previous() 方法。

Example 1

在此示例中,显示了出现在第一个 input 标记之前的每个对象的 name 属性。

from bs4 import BeautifulSoup

fp = open("index.html")
soup = BeautifulSoup(fp, 'html.parser')
tag = soup.find('input')
for t in tag.find_all_previous():
   print (t.name)

Output

form
h1
body
title
head
html

Example 2

在所考虑的 HTML 文档(index.html)中,有三个输入元素。使用以下代码,我们打印 marks.nm 属性之前的 <input> 标记之前所有标记的标记名称。为了区分之前的两个输入标记,我们还会打印 attrs 属性。请注意,其他标记没有任何属性。

from bs4 import BeautifulSoup

fp = open("index.html")
soup = BeautifulSoup(fp, 'html.parser')
tag = soup.find('input', {'name':'marks'})
pretags = tag.find_all_previous()
for pretag in pretags:
   print (pretag.name, pretag.attrs)

Output

input {'type': 'text', 'id': 'age', 'name': 'age'}
input {'type': 'text', 'id': 'nm', 'name': 'name'}
form {}
h1 {}
body {}
title {}
head {}
html {}

Example 3

BeautifulSoup 对象存储了整个文档的树。它没有之前元素,如下例所示:

from bs4 import BeautifulSoup

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

Output

[]