1.BS4模块
1.BS4简介
Beautiful Soup提供一些简单的、python式的函数用来处理导航、搜索、修改分析树等功能。它是一个工具箱,通过解析文档为tiful Soup自动将输入文档转换为Unicode编码,输出文档转换为utf-8编码。
你不需要考虑编码方式,除非文档没有指定一个编一下原始编码方式就可以了。
2.BS4的4种对象
- Tag对象:是html中的一个标签,用BeautifulSoup就能解析出来Tag的具体内容,具体的-格式为‘soup.name‘,其中name是html下的标签。
- BeautifulSoup对象:整个html文本对象,可当作Tag对象
- NavigableString对象:标签内的文本对象
- Comment对象:是一个特殊的NavigableString对象,如果html标签内存在注释,那么它可以过滤掉注释符号保留注释文本
- 最常用的还是BeautifulSoup对象和Tag对象
3.BeautifulSoup对象
1)实例化soup对象
import re
from bs4 import BeautifulSoup
html = """
<html>
<head><title>story12345</title></head>
<body>
<p class="title" name="dromouse"><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"><span>westos</span><!-- Elsie --></a>,
<a href="http://example.com/lacie" class="sister1" 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, 'html.parser')
print(soup.prettify())
lass="story">
Once upon a time there were three little sisters; and their names were
<a class="sister" href="http://example.com/elsie" id="link1">
<span>
westos
</span>
<!-- Elsie -->
</a>
,
<a class="sister1" 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>
{'href': 'http://example.com/elsie', 'class': ['sister'], 'id': 'link1'}
http://example.com/elsie
2)根据标签获取内容
1.标签的常用属性
# ******************标签的常用属性************************
# # 根据格式化, 如果title只有一个, 根据标签可以获取
print(soup.title)
print(type(soup.title))
print(soup.title.name) # 标签的名称
<title>story12345</title>
<class 'bs4.element.Tag'>
title
# 获取标签里面的属性信息
print(soup.a.attrs)
print(soup.a.attrs['href'])
{'href': 'http://example.com/elsie', 'class': ['sister'], 'id': 'link1'}
http://example.com/elsie
2.标签常用的方法
# *******************标签常用的方法*************************
#get方法用于得到标签下的属性值,注意这是一个重要的方法,在许多场合都能用到,比如你要得到<img src=”#”>标签下的图像url,那么就可以用soup.img.get(‘src’)
print(soup.a.get('href'))
print(soup.a.get('class'))
# string得到标签下的文本内容,只有在此标签下没有子标签,或者只有一个子标签的情况下才能返回其中的内容,否则返回的是None;
# get_text()可以获得一个标签中的所有文本内容,包括子孙节点的内容,这是最常用的方法
print(soup.a.string) # 标签里面的内容
print(soup.a.get_text())
http://example.com/elsie
['sister']
None
westos
3.对获取的属性信息进行修改
# *******************对获取的属性信息进行修改***********************
print(soup.a.get('href'))
soup.a['href'] = 'http://www.baidu.com'
print(soup.a.get('href'))
print(soup.a)
http://example.com/elsie
http://www.baidu.com
<a class="sister" href="http://www.baidu.com" id="link1"><span>westos</span><!-- Elsie --></a>
3)面向对象的匹配
# 查找符合条件的所有标签;
aTagObj = soup.find_all('a')
print(aTagObj)
for item in aTagObj:
print(item)
[<a class="sister" href="http://example.com/elsie" id="link1"><span>westos</span><!-- Elsie --></a>, <a class="sister1" href="http://example