BeautifulSoup:
1. 是一个高效的网页解析库,可以从HTML或者XML文件中提取数据
2. 支持不同的解析器,可以对HTML、XML等进行解析
3. 是一个敏感又方便的网页解析库,处理高效,支持多种解析器
4. 利用它在不编写正则表达式的情况下也能方便的实现网页信息的抓取
一、标签选择器:
假设网页返回的HTML文件如下:
h = """
The Dormouse's story The Dormouse's story
Once upon a time there were three little sisters; and their names were,Lacie andTillie;and they lived at the bottom of a well.
...
"""
1 .string --获取文本内容
# 1.导包
from bs4 import BeautifulSoup
# 2.实例化对象
soup = BeautifulSoup(h, 'lxml') # 参数1:要解析的内容 参数2:解析器# 通过标签选取,会返回包含标签本身及其里面的所有内容
print(soup.head) # 包含head标签在内的所有内容
print(soup.p) # 返回匹配的第一个结果# .string是属性,作用是获取字符串文本
print(soup.title.string)
运行结果:
2 .name --获取标签本身名称
from bs4 import BeautifulSoupsoup = BeautifulSoup(h, 'lxml')print(soup.title.name) # 返回标签自身的名称
print(soup.p.name) # 获取标签名
运行结果:
3 .attrs[] --通过属性拿属性的值
from bs4 import BeautifulSoupsoup = BeautifulSoup(h, 'lxml')# 写法1
print(soup.p.attrs['name']) # 获取p标签name属性的属性值
print(soup.a.attrs['href']) # 获取p标签id属性的属性值# 写法2
print(soup.p['id'])
print(soup.p['class']) # 以列表式保存
print(soup.a['href']) # 也是返回第一个值
运行结果:
二、标准选择器
假设网页返回的HTML文件如下:
html='''
Hello
- Foo
- Bar
- Jay
- Foo-2
- Bar-2
'''
1. 使用find_all()根据标签名查找
from bs4 import BeautifulSoup
soup = BeautifulSoup(html, 'lxml')print(soup.find_all('ul')) # 拿到所有ul标签及其内容print(soup.find_all('ul')[0]) # 返回的是列表,可以通过下标获取目标数据print(soup.find_all('div'))
2. get_text() 获取内容
from bs4 import BeautifulSoup
soup = BeautifulSoup(html, 'lxml')for ul in soup.find_all('ul'):print(ul) # 返回的是标签+内容print(ul.get_text()) # 只返回内容
运行结果:
3. 使用find_all()根据属性查找
from bs4 import BeautifulSoup
soup = BeautifulSoup(html, 'lxml')# 特殊属性查找
print(soup.find_all(class_='element')) # class属于Python关键字,做特殊处理 _
print('---------------------------------')# 推荐的查找方法! ---指定标签和属性
print(soup.find_all('li', {'class':'element'}))
print('---------------------------------')
print(soup.find_all('ul', {'id':'list-1'}))
运行结果:
4. text=() 根据文本值选择
from bs4 import BeautifulSoup
soup = BeautifulSoup(html, 'lxml')# 语法格式:text='要查找的文本内容'
print(soup.find_all(text='Foo')) # 可以做内容统计用
print(len(soup.find_all(text='Foo'))) # 统计数量
运行结果:
5. find( name , attrs , recursive , text , **kwargs )
# find返回单个元素,find_all返回所有元素
from bs4 import BeautifulSoup
soup = BeautifulSoup(html, 'lxml')print(soup.find('ul')) # 只返回匹配到的第一个
print(soup.find('page')) # 如果标签不存在返回None
运行结果:
三、CSS选择器
通过select()直接传入CSS选择器即可完成选择
如果对HTML里的CSS选择器很熟悉可以考虑用此方法
假设网页返回的HTML文件如下:
html='''
q321312321
Hello
- Foo
- Bar
- Jay
- Foo
- Bar
'''
from bs4 import BeautifulSoup
soup = BeautifulSoup(html, 'lxml')# 根据标签去找,标签不加任何修饰,多个条件用空格隔开
print(soup.select('ul li'))
print("---------------------------------------")# class类名前加'.'
print(soup.select('.panel-body'))
print("---------------------------------------")# 多个条件用空格隔开
print(soup.select('ul.list'))
print(soup.select('ul .element'))
print("---------------------------------------")# 注意:可以混合使用
# 例如:根据id和class去找
a = soup.select('#list-1 .element') # .select方法会获取满足条件的所有内容
print(a)
for i in a:print(i.string)
获取属性的值:
两种写法:
1,ul['id'] 、
2,ul.attrs['id']
from bs4 import BeautifulSoup
soup = BeautifulSoup(html, 'lxml')s = soup.select('#list-2')
for ul in s:print(ul)# 写法1:print("-------------------------------------------------")print(ul['id'])print("-------------------------------------------------")print(ul['class'])print("-------------------------------------------------")# 写法2:print(ul.attrs['id'])print(ul.attrs['class'])
运行结果:
上一篇:springboot瑞吉外卖
下一篇:化合物应用-动物给药方式