Beautiful Soup - find() 方法



方法描述

Beautiful Soup 中的 find() 方法在该 PageElement 的子元素中查找第一个与给定条件匹配的元素并返回它。

语法

Soup.find(name, attrs, recursive, string, **kwargs)

参数

name - 标签名称过滤器。

attrs - 属性值过滤器字典。

recursive - 如果为 True,则 find() 将执行递归搜索。否则,只考虑直接子元素。

limit - 找到指定数量的匹配项后停止查找。

kwargs - 属性值过滤器字典。

返回值

find() 方法返回 Tag 对象或 NavigableString 对象

示例 1

让我们使用以下 HTML 脚本(作为 index.html)

<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 的元素

from bs4 import BeautifulSoup

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

obj = soup.find(id = 'nm')
print (obj)

输出

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

示例 2

find() 方法返回解析后的文档中第一个具有给定属性的标签。

obj = soup.find(attrs={"name":'marks'})

输出

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

示例 3

如果 find() 找不到任何内容,则返回 None

obj = soup.find('dummy')
print (obj)

输出

None
广告

© . All rights reserved.