《学习笔记58》—— python中的Beautifulsoup库基本用法

python中的Beautifulsoup库

介绍:

​ Beautifulsoup是一个可以从HTML或XML文件中提取数据的Python库.它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文档的方式。

引入:

首先要安装bs4库。
在这里插入图片描述

from bs4 import BeautifulSoup 

简单使用:

from bs4 import BeautifulSoup
file = open('./baidu.html','rb')  # 打开一个html文件
html = file.read()
soup = BeautifulSoup(html,'html.parser')  # 创建Beautifulsoup对象

new_html = soup.prettify() # 格式化html结构
get_title = soup.title  # 获取title标签

BeautifulSoup有四大对象种类,以下我们将一一介绍。

  • Tag
  • NavigableString
  • BeautifulSoup
  • Comment

1. BeautifulSoup4四大对象种类

1.1 Tag

介绍:

​ Tag通俗点讲就是HTML中的标签(有时候也叫做元素),我们可以利用soup对象加标签名轻松地获取这些标签的内容,这些对象的类型是bs4.element.Tag。但是注意,它查找的是在所有内容中的第一个符合要求的标签

语法:

soup对象.标签名

实例:

from bs4 import BeautifulSoup
file = open('./baidu.html','rb')  # 打开一个html文件
html = file.read()
soup = BeautifulSoup(html,'html.parser')  # 创建Beautifulsoup对象

#获取title标签的所有内容
print(soup.title)
#获取head标签的所有内容
print(soup.head)
#获取第一个a标签的所有内容
print(soup.a)

#查看获取到的内容的类型
print(type(soup.a))

​ 对于Tag,它有两个重要的属性,是name和attrs。

​ soup 对象本身的 name 为[document],对于其他标签的 name 为标签本身的名称。

​ attrs 属性可以获取标签的属性。返回的类型是字典。

实例:

from bs4 import BeautifulSoup
file = open('./baidu.html','rb')  # 打开一个html文件
html = file.read()
soup = BeautifulSoup(html,'html.parser')  # 创建Beautifulsoup对象

#获取soup对象的名称
print(soup.name)  # [document]
#获取第一个a标签的名称
print(soup.a.name)  # a

#获取第一个a标签的所有属性
print(soup.a.attrs)  # {'class': ['toindex'], 'href': '/'}

1.2 NavigableString

介绍:

​ 既然我们已经得到了标签的内容,那么问题来了,我们要想获取标签内部的文字怎么办呢?很简单,用 .string 即可。

语法:

soup对象.标签名.string

实例:

from bs4 import BeautifulSoup
file = open('./baidu.html','rb')  # 打开一个html文件
html = file.read()
soup = BeautifulSoup(html,'html.parser')  # 创建Beautifulsoup对象

#获取title标签的内部文字
print(soup.title.string)  # 百度一下,你就知道
#获取第一个a标签的内部文字
print(soup.a.string)  # 百度首页

1.3 BeautifulSoup

介绍:

​ BeautifulSoup对象表示的是一个文档的内容。大部分时候,可以把它当作 Tag 对象,是一个特殊的 Tag,我们可以分别获取它的类型,名称,以及属性。

实例:

from bs4 import BeautifulSoup
file = open('./baidu.html','rb')  # 打开一个html文件
html = file.read()
soup = BeautifulSoup(html,'html.parser')  # 创建Beautifulsoup对象

print(soup.name)  # [document]
print(type(soup.name))  # <class 'str'>
print(soup.attrs)  # {}

1.4 Comment

介绍:

​ Comment 对象是一个特殊类型的 NavigableString 对象,其输出的内容不包括注释符号。

2. 遍历文档树

2.1 .contents

介绍:

​ 我们可以使用.contents来获取Tag的所有子节点,返回一个列表。

语法:

soup对象.标签名.contents

实例:

from bs4 import BeautifulSoup
file = open('./baidu.html','rb')  # 打开一个html文件
html = file.read()
soup = BeautifulSoup(html,'html.parser')  # 创建Beautifulsoup对象

#获取head标签的所有子节点
head_list = soup.head.contents
#获取head标签子节点列表的第一个元素
print(head_list[0])  # <meta content="text/html;charset=utf-8" http-equiv="Content-Type"/>

2.2 children

介绍:

​ 获取Tag的所有子节点,返回一个生成器。

语法:

soup对象.标签名.children

示例:

from bs4 import BeautifulSoup
file = open('./baidu.html','rb')  # 打开一个html文件
html = file.read()
soup = BeautifulSoup(html,'html.parser')  # 创建Beautifulsoup对象

#获取head标签的所有子节点,返回一个生成器
print(soup.head.children)  # <list_iterator object at 0x0000024BC45D4E50>

2.3 .descendants

介绍:

​ 获取Tag的所有子孙节点。

2.4 .strings

介绍:

​ 如果Tag包含多个字符串,即在子孙节点中有内容,可以用此获取,而后进行遍历。

2.5 .stripped_strings

介绍:

​ 与strings用法一致,只不过可以去除掉那些多余的空白内容。

2.6 .parent

介绍:

​ 获取Tag的父节点。

2.7 .parents

介绍:

​ 递归得到父辈元素的所有节点,返回一个生成器。

2.8 .previous_sibling

介绍:

​ 获取当前Tag的上一个节点,属性通常是字符串或空白,真实结果是当前标签与上一个标签之间的顿号和换行符。

2.9 .next_sibling

介绍:

​ 获取当前Tag的下一个节点,属性通常是字符串或空白,真是结果是当前标签与下一个标签之间的顿号与换行符。

3. 搜索文档树

3.1 find_all(name,attrs,recursive,text,**kwaargs)的用法

3.1.1 name参数

参数类型:

  1. 字符串过滤:会查找与字符串完全匹配的内容

实例:

from bs4 import BeautifulSoup
file = open('./baidu.html','rb')  # 打开一个html文件
html = file.read()
soup = BeautifulSoup(html,'html.parser')  # 创建Beautifulsoup对象

a_list = soup.find_all('a')  # 会返回包含所有a标签的列表
print(a_list)

参数类型:

​ 2.正则表达式过滤:如果传入的是正则表达式,那么BeautifulSoup会通过search()来匹配内容

实例:

from bs4 import BeautifulSoup
import re  # 引入正则表达式库
file = open('./baidu.html','rb')  # 打开一个html文件
html = file.read()
soup = BeautifulSoup(html,'html.parser')  # 创建Beautifulsoup对象

get_a = re.compile("a")  # 创建a字符串的正则匹配规则
a_list = soup.find_all(get_a)  # 获得一个包含a字符串的列表
for item in a_list:  # 循环遍历列表内容
    print(item)

参数类型:

  3. 列表:如果传入一个列表,BeautifulSoup将会与列表中的任一元素匹配到的节点返回
from bs4 import BeautifulSoup
file = open('./baidu.html','rb')  # 打开一个html文件
html = file.read()
soup = BeautifulSoup(html,'html.parser')  # 创建Beautifulsoup对象

m_l_list = soup.find_all(['meta','link'])  # 传入列表作为参数时,返回的也是一个列表
print(m_l_list)
for item in m_l_list:
    print(item)

参数类型:

  4. 方法:传入一个方法,根据方法来匹配

实例:

from bs4 import BeautifulSoup
file = open('./baidu.html','rb')  # 打开一个html文件
html = file.read()
soup = BeautifulSoup(html,'html.parser')  # 创建Beautifulsoup对象

def name_is_exists(tag):
    return tag.has_attr('name')  # 返回包含了name属性的标签

t_list = soup.find_all(name_is_exists)
print(t_list)
3.1.2 kwargs参数
from bs4 import BeautifulSoup
file = open('./baidu.html','rb')  # 打开一个html文件
html = file.read()
soup = BeautifulSoup(html,'html.parser')  # 创建Beautifulsoup对象

# 查询id=head的Tag
head_list = soup.find_all(id="head")
print(head_list)

# 查询所有包含calss的Tag(注意:class在python中属于关键字,所以需要加上_以示区分)
class_list = soup.find_all(class_=True)
print(class_list)
3.1.3 attrs参数

介绍:

​ 用attrs参数,定义一个字典来搜索包含特殊属性的tag

实例:

from bs4 import BeautifulSoup
file = open('./baidu.html','rb')  # 打开一个html文件
html = file.read()
soup = BeautifulSoup(html,'html.parser')  # 创建Beautifulsoup对象

t_list = soup.find_all(attrs={'class':'s-top-wrap'})  # 搜索包含“s-top-wrap”这一class属性的标签,返回一个列表
print(t_list)
3.1.4 text参数

介绍:

​ 通过text参数可以搜索文档中的字符串内容,与name参数的可选值一样,text参数接受字符串、正则表达式、列表。

实例:

from bs4 import BeautifulSoup
file = open('./baidu.html','rb')  # 打开一个html文件
html = file.read()
soup = BeautifulSoup(html,'html.parser')  # 创建Beautifulsoup对象

a_list = soup.find_all(text='百度一下,你就知道')  # 搜索标签中是否有“百度一下,你就知道”内容,有的话则返回该内容的列表
print(a_list)
3.1.5 limit参数

介绍:

​ 可以传入一个limit参数来限制返回的数量,当搜索出的数据量为5,而设置了limit=2时,此时只会返回前2个数据。

from bs4 import BeautifulSoup
file = open('./baidu.html','rb')  # 打开一个html文件
html = file.read()
soup = BeautifulSoup(html,'html.parser')  # 创建Beautifulsoup对象

t_list = soup.find_all('a',limit=2)  # 只会返回前两个包含a标签的列表
print(t_list)

3.2 fin()

介绍:

​ find()方法与find_all()方法相似,但它只会返回符合条件的第一个Tag,有时我们只需要一个Tag时,我们就可以用到find()方法了。

​ 如果未搜索到值时,将返回一个None。

​ 这里就不再演示用法。

CSS选择器

介绍:

​ BeautifulSoup支持发部分的CSS选择器,在Tag获取BeautifulSoup对象的.select()方法中传入字符串参数,即可使用CSS选择器的语法找到Tag。

​ CSS选择器属于CSS基本语法,这里不作演示。

语法:

soup对象.select(‘css选择器’)

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Merrill He

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值