Beautiful Soup 简明教程
Beautiful Soup - find vs find_all
Beautiful Soup 库包括 find() 和 find_all() 方法。解析 HTML 或 XML 文档时,两种方法都是最常使用的方法之一。从特定的文档树中,你常常需要找到一个确定标记类型的 PageElement,或具有某些属性,或具有特定 CSS 样式等。这些条件作为参数提供给 find() 和 find_all() 方法。两者的主要区别点在于,find() 找到满足条件的第一个子元素,而 find_all() 方法搜索条件的所有子元素。
Beautiful Soup library includes find() as well as find_all() methods. Both methods are one of the most frequently used methods while parsing HTML or XML documents. From a particular document tree You often need to locate a PageElement of a certain tag type, or having certain attributes, or having a certain CSS style etc. These criteria are given as argument to both find() and find_all() methods. The main point of difference between the two is that while find() locates the very first child element that satisfies the criteria, find_all() method searches for all the children elements of the criteria.
find() 方法的定义如下语法 −
The find() method is defined with following syntax −
Syntax
find(name, attrs, recursive, string, **kwargs)
name 参数指定标记名称的筛选器。使用 attrs,可以针对标记属性值设置筛选器。如果 recursive 参数为 True,则会执行递归搜索。你可以将可变 kwargs 传递为属性值筛选器的字典。
The name argument specifies a filter on tag name. With attrs, a filter on tag attribute values can be set up. The recursive argument forces a recursive search if it is True. You can pass variable kwargs as dictionary of filters on attribute values.
soup.find(id = 'nm')
soup.find(attrs={"name":'marks'})
find_all() 方法接收与 find() 方法完全一样所有的参数,此外还有 limit 参数。这是一个整数,它将搜索限制为给定筛选器条件指定数量的匹配项。如果没有设置,find_all() 将在该 PageElement 下的所有子元素中搜索条件。
The find_all() method takes all the arguments as for the find() method, in addition there is a limit argument. It is an integer, restricting the search the specified number of occurrences of the given filter criteria. If not set, find_all() searches for the criteria among all the children under the said PageElement.
soup.find_all('input')
lst=soup.find_all('li', limit =2)
如果将 find_all() 方法的 limit 参数设置为 1,它将虚拟充当 find() 方法。
If the limit argument for find_all() method is set to 1, it virtually acts as find() method.
两种方法的返回类型不同。find() 方法返回最早找到的一个 Tag 对象或 NavigableString 对象。find_all() 方法返回一个 ResultSet,其中包含满足筛选器条件的所有 PageElement。
The return type of both the methods differs. The find() method returns either a Tag object or a NavigableString object first found. The find_all() method returns a ResultSet consisting of all the PageElements satisfying the filter criteria.
下面是一个演示 find 和 find_all 方法之间的区别的示例。
Here is an example that demonstrates the difference between find and find_all methods.
Example
from bs4 import BeautifulSoup
markup =open("index.html")
soup = BeautifulSoup(markup, 'html.parser')
ret1 = soup.find('input')
ret2 = soup.find_all ('input')
print (ret1, 'Return type of find:', type(ret1))
print (ret2)
print ('Return tyoe find_all:', type(ret2))
#set limit =1
ret3 = soup.find_all ('input', limit=1)
print ('find:', ret1)
print ('find_all:', ret3)
Output
<input id="nm" name="name" type="text"/> Return type of find: <class 'bs4.element.Tag'>
[<input id="nm" name="name" type="text"/>, <input id="age" name="age" type="text"/>, <input id="marks" name="marks" type="text"/>]
Return tyoe find_all: <class 'bs4.element.ResultSet'>
find: <input id="nm" name="name" type="text"/>
find_all: [<input id="nm" name="name" type="text"/>]