Beautiful Soup 简明教程
Beautiful Soup - Find Element using CSS Selectors
在 Beautiful Soup 库中,select() 方法是用于抓取 HTML/XML 文档的重要工具。类似于 find() 和其他 find_*() 方法,select() 方法还有助于查找符合给定条件的元素。但是,find*() 方法根据标记名称及其属性搜索 PageElements,select() 方法根据给定的 CSS 选择器搜索文档树。
Beautiful Soup 还具有 select_one() 方法。select() 和 select_one() 之间的区别在于,select() 返回属于 PageElement 并由 CSS 选择器表征的所有元素的 ResultSet;而 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 对象的 ResultSet,但 select_one() 方法返回一个单个 Tag 对象。
Soup Sieve Library
Soup Sieve 是一款 CSS 选择器库。它已与 Beautiful Soup 4 集成,因此可随 Beautiful Soup 包一起安装。它提供使用现代 CSS 选择器选择、匹配和筛选文档树标记的能力。Soup Sieve 目前实现了 CSS 1 级规范到 CSS 4 级的大部分 CSS 选择器,但除了一些尚未实现的例外情况。
Soup Sieve 库具有不同类型的 CSS 选择器。基本的 CSS 选择器为 −
Example
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)
Example
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)
Example
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)
Example
from bs4 import BeautifulSoup
html = '''
<h1>Tutorialspoint Online Library</h1>
<p><b>It's all Free</b></p>
<a class="prog" href="https://www.tutorialspoint.com/java/java_overview.htm" id="link1">Java</a>
<a class="prog" href="https://www.tutorialspoint.com/cprogramming/index.htm" id="link2">C</a>
'''
soup = BeautifulSoup(html, 'html5lib')
print(soup.select('[href]'))
Pseudo Classes
CSS 规范定义了多个伪 CSS 类。伪类是添加到选择器中的关键字,用于定义所选元素的特殊状态。它会对现有元素添加效果。例如,:link 选择尚未访问的链接(带 href 属性的每个 <a> 和 <area> 元素)。
nth-of-type 和 nth-child 伪类选择器被广泛使用。
:nth-of-type()
:nth-of-type() 选择器根据元素在同级元素组中的位置匹配指定类型的元素。关键字 even 和 odd 将分别从子同级元素组中选择元素。
在以下示例中,选择了 <p> 类型的第二个元素。
Example
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)'))