主要内容:BeautifulSoup的常用查找方法及参数介绍。
<>.find_all(name,attrs,recursive,string,**kwargs):返回列表类型,存储查找结果,可用于BeautifulSoup对象和标签。
name:对标签名称的检索字符串
attrs:对标签属性进行检索可标注属性检索
recursive:是否对子孙全部索引,默认为True
string:对标签中的字符全区域进行检索
测试字符串:
#查找方法
html_doc = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title"><b>The Dormouse's story</b></p>
<p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>
<p class="story">...</p>
"""
soup = BeautifulSoup(html_doc,'html.parser')
#通过标签名name参数查找
soup.find_all('a')
输出所有<a>标签的列表:[<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>,
<a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>,
<a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]
#获取所有a标签的超链接
for i in soup.find_all('a'):
print(i.attrs.get('href'))
输出:
#通过属性查找
import re
print('----',soup.find_all('p','title')) #属性为title的p标签
print('----',soup.find_all(id='link1'))
print('----',soup.find_all(id=re.compile('link'))) #正则匹配id含有link字符串的标签
输出:
---- [<p class="title"><b>The Dormouse's story</b></p>]
---- [<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>]
---- [<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>, <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>, <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]
#是否对于子孙全部索引
print('----',soup.find_all('a','sister'))
print('----',soup.find_all('a','sister',recursive=False))
输出:---- [<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>, <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>, <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]
---- []
#标签中的字符串查找
print('----',soup.find_all(string='''The Dormouse's story'''))
print('----',soup.find_all(string=re.compile('The\w*')))
输出:---- ["The Dormouse's story", "The Dormouse's story"]
---- ["The Dormouse's story", "The Dormouse's story"]
补充方法:
<>.find():搜索且只返回第一个找到的结果,参数同find_all()
<>.find_parent():在先辈节点中搜索,返回一个结果,参数同find_all()
<>.find_parents():在先辈节点中搜索,返回列表,参数同find_all()
<>.find_next_sibiling():在后序平行节点中搜索,返回一个结果,参数同find_all()
<>.find_next_sibilings():在后序平行节点中搜索,返回列表,参数同find_all()
<>.find_previous_sibiling():在前序平行节点中搜索,返回一个结果,参数同find_all()
<>.find_previous_sibilings():在前序平行节点中搜索,返回一个列表,参数同find_all()