Beautiful Soup 简明教程
Beautiful Soup - find() Method
Method Description
Beautiful Soup 中的 find() 方法在该 PageElement 的子元素中查找与给定标准匹配的第一个 Element 并返回它。
The find() method in Beautiful Soup looks for the first Element that matches the given criteria in the children of this PageElement and returns it.
Parameters
name − 对标记名称的筛选。
name − A filter on tag name.
attrs − 对属性值进行筛选的字典。
attrs − A dictionary of filters on attribute values.
recursive − 如果为 True,则 find() 将执行递归搜索。否则,仅考虑直接子元素。
recursive − If this is True, find() a recursive search will be performed. Otherwise, only the direct children will be considered.
limit − 在找到指定数量的出现次数后停止寻找。
limit − Stop looking after specified number of occurrences have been found.
kwargs − 对属性值进行筛选的字典。
kwargs − A dictionary of filters on attribute values.
Return value
find() 方法返回 Tag 对象或 NavigableString 对象
The find() method returns Tag object or a NavigableString object
Example 1
让我们为了这个目的使用以下 HTML 脚本(作为 index.html):
Let us use the following HTML script (as index.html) for the purpose
<html>
<head>
<title>TutorialsPoint</title>
</head>
<body>
<form>
<input type = 'text' id = 'nm' name = 'name'>
<input type = 'text' id = 'age' name = 'age'>
<input type = 'text' id = 'marks' name = 'marks'>
</form>
</body>
</html>
下面的 Python 代码找到了 id 为 nm 的元素:
The following Python code finds the element with its id as nm
from bs4 import BeautifulSoup
fp = open("index.html")
soup = BeautifulSoup(fp, 'html.parser')
obj = soup.find(id = 'nm')
print (obj)
Example 2
find() 方法返回已解析文档中具有给定属性的第一个标签。
The find() method returns the first tag in the parsed document that has the given attributes.
obj = soup.find(attrs={"name":'marks'})