- Beautiful Soup 教程
- Beautiful Soup - 首页
- Beautiful Soup - 概述
- Beautiful Soup - 网页抓取
- Beautiful Soup - 安装
- Beautiful Soup - 解析页面
- Beautiful Soup - 对象类型
- Beautiful Soup - 检查数据源
- Beautiful Soup - 抓取 HTML 内容
- Beautiful Soup - 通过标签导航
- Beautiful Soup - 通过 ID 查找元素
- Beautiful Soup - 通过 Class 查找元素
- Beautiful Soup - 通过属性查找元素
- Beautiful Soup - 搜索树
- Beautiful Soup - 修改树
- Beautiful Soup - 解析文档的一部分
- Beautiful Soup - 查找元素的所有子元素
- Beautiful Soup - 使用 CSS 选择器查找元素
- Beautiful Soup - 查找所有注释
- Beautiful Soup - 从 HTML 中抓取列表
- Beautiful Soup - 从 HTML 中抓取段落
- BeautifulSoup - 从 HTML 中抓取链接
- Beautiful Soup - 获取所有 HTML 标签
- Beautiful Soup - 获取标签内部的文本
- Beautiful Soup - 查找所有标题
- Beautiful Soup - 提取标题标签
- Beautiful Soup - 提取电子邮件 ID
- Beautiful Soup - 抓取嵌套标签
- Beautiful Soup - 解析表格
- Beautiful Soup - 选择第 n 个子元素
- Beautiful Soup - 搜索标签内部的文本
- Beautiful Soup - 删除 HTML 标签
- Beautiful Soup - 删除所有样式
- Beautiful Soup - 删除所有脚本
- Beautiful Soup - 删除空标签
- Beautiful Soup - 删除子元素
- Beautiful Soup - find 与 find_all 的区别
- Beautiful Soup - 指定解析器
- Beautiful Soup - 比较对象
- Beautiful Soup - 复制对象
- Beautiful Soup - 获取标签位置
- Beautiful Soup - 编码
- Beautiful Soup - 输出格式化
- Beautiful Soup - 美化输出
- Beautiful Soup - NavigableString 类
- Beautiful Soup - 将对象转换为字符串
- Beautiful Soup - 将 HTML 转换为文本
- Beautiful Soup - 解析 XML
- Beautiful Soup - 错误处理
- Beautiful Soup - 故障排除
- Beautiful Soup - 移植旧代码
- Beautiful Soup - 函数参考
- Beautiful Soup - contents 属性
- Beautiful Soup - children 属性
- Beautiful Soup - string 属性
- Beautiful Soup - strings 属性
- Beautiful Soup - stripped_strings 属性
- Beautiful Soup - descendants 属性
- Beautiful Soup - parent 属性
- Beautiful Soup - parents 属性
- Beautiful Soup - next_sibling 属性
- Beautiful Soup - previous_sibling 属性
- Beautiful Soup - next_siblings 属性
- Beautiful Soup - previous_siblings 属性
- Beautiful Soup - next_element 属性
- Beautiful Soup - previous_element 属性
- Beautiful Soup - next_elements 属性
- Beautiful Soup - previous_elements 属性
- Beautiful Soup - find 方法
- Beautiful Soup - find_all 方法
- Beautiful Soup - find_parents 方法
- Beautiful Soup - find_parent 方法
- Beautiful Soup - find_next_siblings 方法
- Beautiful Soup - find_next_sibling 方法
- Beautiful Soup - find_previous_siblings 方法
- Beautiful Soup - find_previous_sibling 方法
- Beautiful Soup - find_all_next 方法
- Beautiful Soup - find_next 方法
- Beautiful Soup - find_all_previous 方法
- Beautiful Soup - find_previous 方法
- Beautiful Soup - select 方法
- Beautiful Soup - append 方法
- Beautiful Soup - extend 方法
- Beautiful Soup - NavigableString 方法
- Beautiful Soup - new_tag 方法
- Beautiful Soup - insert 方法
- Beautiful Soup - insert_before 方法
- Beautiful Soup - insert_after 方法
- Beautiful Soup - clear 方法
- Beautiful Soup - extract 方法
- Beautiful Soup - decompose 方法
- Beautiful Soup - replace_with 方法
- Beautiful Soup - wrap 方法
- Beautiful Soup - unwrap 方法
- Beautiful Soup - smooth 方法
- Beautiful Soup - prettify 方法
- Beautiful Soup - encode 方法
- Beautiful Soup - decode 方法
- Beautiful Soup - get_text 方法
- Beautiful Soup - diagnose 方法
- Beautiful Soup 有用资源
- Beautiful Soup - 快速指南
- Beautiful Soup - 有用资源
- Beautiful Soup - 讨论
Beautiful Soup - 使用 CSS 选择器查找元素
在 Beautiful Soup 库中,select() 方法是用于抓取 HTML/XML 文档的重要工具。与 find() 和其他 find_*() 方法类似,select() 方法也有助于定位满足给定条件的元素。但是,find*() 方法根据标签名称及其属性搜索 PageElements,而 select() 方法根据给定的 CSS 选择器搜索文档树。
Beautiful Soup 还具有 select_one() 方法。select() 和 select_one() 的区别在于,select() 返回属于 PageElement 并以 CSS 选择器为特征的所有元素的结果集;而 select_one() 返回满足基于 CSS 选择器选择标准的元素的第一个匹配项。
在 Beautiful Soup 4.7 版本之前,select() 方法只能支持常用的 CSS 选择器。随着 4.7 版本的发布,Beautiful Soup 集成了 Soup Sieve CSS 选择器库。因此,现在可以使用更多选择器。在 4.12 版本中,除了现有的便利方法 select() 和 select_one() 之外,还添加了一个 .css 属性。select() 方法的参数如下:
select(selector, limit, **kwargs)
selector − 包含 CSS 选择器的字符串。
limit − 找到此数量的结果后,停止查找。
kwargs − 要传递的关键字参数。
如果 limit 参数设置为 1,则它等效于 select_one() 方法。select() 方法返回 Tag 对象的结果集,而 select_one() 方法返回单个 Tag 对象。
Soup Sieve 库
Soup Sieve 是一个 CSS 选择器库。它已与 Beautiful Soup 4 集成,因此与 Beautiful Soup 包一起安装。它提供了使用现代 CSS 选择器选择、匹配和过滤文档树标签的功能。Soup Sieve 目前实现了从 CSS 级别 1 规范到 CSS 级别 4 的大多数 CSS 选择器,除了某些尚未实现的选择器。
Soup Sieve 库具有不同类型的 CSS 选择器。基本的 CSS 选择器包括:
类型选择器
通过节点名称匹配元素。例如:
tags = soup.select('div')
示例
from bs4 import BeautifulSoup, NavigableString
markup = '''
<div id="Languages">
<p>Java</p> <p>Python</p> <p>C++</p>
</div>
'''
soup = BeautifulSoup(markup, 'html.parser')
tags = soup.select('div')
print (tags)
输出
[<div id="Languages"> <p>Java</p> <p>Python</p> <p>C++</p> </div>]
通用选择器 (*)
它匹配任何类型的元素。例如:
tags = soup.select('*')
ID 选择器
它根据元素的 id 属性匹配元素。符号 # 表示 ID 选择器。例如:
tags = soup.select("#nm")
示例
from bs4 import BeautifulSoup
html = '''
<form>
<input type = 'text' id = 'nm' name = 'name'>
<input type = 'text' id = 'age' name = 'age'>
<input type = 'text' id = 'marks' name = 'marks'>
</form>
'''
soup = BeautifulSoup(html, 'html.parser')
obj = soup.select("#nm")
print (obj)
输出
[<input id="nm" name="name" type="text"/>]
类选择器
它根据 class 属性中包含的值匹配元素。以 . 为前缀的类名称是 CSS 类选择器。例如:
tags = soup.select(".submenu")
示例
from bs4 import BeautifulSoup, NavigableString
markup = '''
<div id="Languages">
<p>Java</p> <p>Python</p> <p>C++</p>
</div>
'''
soup = BeautifulSoup(markup, 'html.parser')
tags = soup.select('div')
print (tags)
输出
[<div id="Languages"> <p>Java</p> <p>Python</p> <p>C++</p> </div>]
属性选择器
属性选择器根据元素的属性匹配元素。
soup.select('[attr]')
示例
from bs4 import BeautifulSoup
html = '''
<h1>Tutorialspoint Online Library</h1>
<p><b>It's all Free</b></p>
<a class="prog" href="https://tutorialspoint.com/java/java_overview.htm" id="link1">Java</a>
<a class="prog" href="https://tutorialspoint.com/cprogramming/index.htm" id="link2">C</a>
'''
soup = BeautifulSoup(html, 'html5lib')
print(soup.select('[href]'))
输出
[<a class="prog" href="https://tutorialspoint.com/java/java_overview.htm" id="link1">Java</a>, <a class="prog" href="https://tutorialspoint.com/cprogramming/index.htm" id="link2">C</a>]
伪类
CSS 规范定义了许多伪 CSS 类。伪类是添加到选择器中的关键字,用于定义所选元素的特殊状态。它为现有元素添加效果。例如,:link 选择尚未访问的链接(每个 <a> 和 <area> 元素,且具有 href 属性)。
伪类选择器 nth-of-type 和 nth-child 被广泛使用。
:nth-of-type()
选择器 :nth-of-type() 根据其在一个兄弟元素组中的位置匹配给定类型的元素。关键字 even 和 odd 将分别从兄弟元素的子组中选择元素。
在以下示例中,选择了 <p> 类型的第二个元素。
示例
from bs4 import BeautifulSoup
html = '''
<p id="0"></p>
<p id="1"></p>
<span id="2"></span>
<span id="3"></span>
'''
soup = BeautifulSoup(html, 'html5lib')
print(soup.select('p:nth-of-type(2)'))
输出
[<p id="1"></p>]
:nth-child()
此选择器根据其在一个兄弟元素组中的位置匹配元素。关键字 even 和 odd 将分别选择其位置在兄弟元素组中为偶数或奇数的元素。
用法
:nth-child(even) :nth-child(odd) :nth-child(2)
示例
from bs4 import BeautifulSoup, NavigableString
markup = '''
<div id="Languages">
<p>Java</p> <p>Python</p> <p>C++</p>
</div>
'''
soup = BeautifulSoup(markup, 'html.parser')
tag = soup.div
child = tag.select_one(':nth-child(2)')
print (child)
输出
<p>Python</p>