什么是BeautifulSoup
Beautiful Soup 是一个可以从HTML或XML文件中提取数据的Python库.它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文档的方式.
多看官方文档https://beautifulsoup.readthedocs.io/zh_CN/latest/
通过例子来讲解bs库的用法
from bs4 import BeautifulSoup
html = '''
<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,'lxml') //将代码进行格式化
output:
<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 class\="sister" href\="http://example.com/elsie" id\="link1"\>
Elsie
</a\>,
<a class\="sister" href\="http://example.com/lacie" id\="link2"\>
Lacie
</a\>and
<a class\="sister" href\="http://example.com/tillie" id\="link3"\>
Tillie
</a\>
;
and they lived at the bottom of a well.
</p\>
<p class\="story"\>
...
</p\>
</body\>
</html\>
详解bs4对html的解析
Tag
bs的使用和字典的使用极为相似,用.来进行运算
- print(soup.title) 获取title标签(灵活变话:soup.a soup.li等
<title>The Dormouse's story</title>
- print(soup.title.string) 获取title中的内容(可以灵活变化,获得其他标签中的内容)
The Dormouse's story
- print(soup.p["class"]) 获取class的值 (也可以灵活变换获取id或者data-id的值)
\['title'\]
- print(soup.find(id = 'link3')) 利用find函数找到相应的标签
bs可以多次调用得到需要的标签内容
<p class\="title"\>
<b\>
The Dormouse's story
</b\>
</p\>
input:print(soup.p.b.string)
output: The Dormouse's story
find_all
find_all( name , attrs , recursive , string , **kwargs )
-
name: 根据标签名来进行查询(常用)
- soup.find_all('a') find_all返回的是一个列表,相应的可以soup.find_all('li')
常用方法是将列表中的元素提取出来进行处理
alist = soup.find\_all('a')
for a in alist:
function(a)
- 根据attrs来进行查询,用传入字典的方式来精准查询
html\='''
<div class="panel">
<div class="panel-heading">
<h4>Hello</h4>
</div>
<div class="panel-body">
<ul class="list" id="list-1" name="elements">
<li class="element">Foo</li>
<li class="element">Bar</li>
<li class="element">Jay</li>
</ul>
<ul class="list list-small" id="list-2">
<li class="element">Foo</li>
<li class="element">Bar</li>
</ul>
</div>
</div>
'''
from bs4 import BeautifulSoup
soup = BeautifulSoup(html, 'lxml')
print(soup.find\_all(attrs\={'id': 'list-1'}))
print(soup.find\_all(attrs\={'name': 'elements'}))
上面两句的output:
\[<ul class\="list" id\="list-1" name\="elements"\>
<li class\="element"\>Foo</li\>
<li class\="element"\>Bar</li\>
<li class\="element"\>Jay</li\>
</ul\>\]
- keyword参数
用tag的属性来进行搜索,搜索每个tag的id属性
soup.find_all(id = 'list-2')
class是特殊字,用下面方法进行处理
soup.find_all('',{"class":"element"})
- 按照CSS搜索
可以用class_ = ...... 来对class属性进行搜索新属性
soup.find_all("div",class_ = "panel-body")
谷歌浏览器快速获得标签CSS选择器方法
用选择器对组件选择---->找到相应的语句----->右键------>
可以根据需要进行copy,selector即为CSS的路径
find_all()若没有找到相应的数据返回一个空的列表
find()则返回一个None
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。