python 中的 BeautifulSoup 网页使用方法解析
作者:autofelix 发布时间:2022-06-25 19:09:26
标签:python,BeautifulSoup,网页
一、安装
Bautiful Soup 是第三方库,因此需要单独下载,下载方式非常简单
由于 BS4 解析页面时需要依赖文档解析器,所以还需要安装 lxml 作为解析库
Python 也自带了一个文档解析库 html.parser, 但是其解析速度要稍慢于 lxml
pip install bs4
pip install lxml
pip install html5lib
二、html.parser解析
html.parser 表示解析文档时所用的解析器
解析器也可以是 lxml 或者 html5lib
html = '''
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Modal title</h4>
</div>
<div class="modal-body">
...
</div>
<div class="modal-footer">
<a href="#" rel="external nofollow" rel="external nofollow" class="btn btn-default" data-dismiss="modal">Close</a>
<a href="#" rel="external nofollow" rel="external nofollow" class="btn btn-primary">Save</a>
</div>
</div>
</div>
'''
from bs4 import BeautifulSoup
soup = BeautifulSoup(html, 'html.parser')
#prettify()用于格式化输出html/xml文档
print(soup.prettify())
三、外部文档解析
外部文档,您也可以通过 open 的方式打开读取
from bs4 import BeautifulSoup
fp = open('html_doc.html', encoding='utf8')
soup = BeautifulSoup(fp, 'lxml')
四、标签选择器
标签(Tag)是组成 HTML 文档的基本元素
通过标签名和标签属性可以提取出想要的内容
from bs4 import BeautifulSoup
soup = BeautifulSoup('<p class="name nickname user"><b>i am autofelix</b></p>', 'html.parser')
#获取整个p标签的html代码
print(soup.p)
#获取b标签
print(soup.p.b)
#获取p标签内容,使用NavigableString类中的string、text、get_text()
print(soup.p.text)
#返回一个字典,里面是多有属性和值
print(soup.p.attrs)
#查看返回的数据类型
print(type(soup.p))
#根据属性,获取标签的属性值,返回值为列表
print(soup.p['class'])
#给class属性赋值,此时属性值由列表转换为字符串
soup.p['class']=['Web','Site']
print(soup.p)
五、css选择器
支持大部分的 CSS 选择器,比如常见的标签选择器、类选择器、id 选择器,以及层级选择器
通过向 select 方法中添加选择器,就可以在 HTML 文档中搜索到与之对应的内容
html = """
<html>
<head>
<title>零基础学编程</title>
</head>
<body>
<p class="intro"><b>i am autofelix</b></p>
<p class="nickname">飞兔小哥</p>
<a href="https://autofelix.blog.csdn.net" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="csdn">csdn主页</a>
<a href="https://xie.infoq.cn/u/autofelix/publish" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="infoq">infoq主页</a>
<a href="https://blog.51cto.com/autofelix" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="51cto">51cto主页</a>
<p class="attention">跪求关注 一键三连</p>
<p class="introduce">
<a href="https://www.cnblogs.com/autofelix" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="cnblogs">博客园主页</a>
</p>
</body>
</html>
"""
from bs4 import BeautifulSoup
soup = BeautifulSoup(html, 'html.parser')
#根据元素标签查找
print(soup.select('nickname'))
#根据属性选择器查找
print(soup.select('a[href]'))
#根据类查找
print(soup.select('.attention'))
#后代节点查找
print(soup.select('html head title'))
#查找兄弟节点
print(soup.select('p + a'))
#根据id选择p标签的兄弟节点
print(soup.select('p ~ #csdn'))
#nth-of-type(n)选择器,用于匹配同类型中的第n个同级兄弟元素
print(soup.select('p ~ a:nth-of-type(1)'))
#查找子节点
print(soup.select('p > a'))
print(soup.select('.introduce > #cnblogs'))
六、节点遍历
可以使用 contents、children 用来遍历子节点
可以使用 parent 与 parents 用来遍历父节点
可以使用 next_sibling 与 previous_sibling 用来遍历兄弟节点
html = """
<html>
<head>
<title>零基础学编程</title>
</head>
<body>
<p class="intro"><b>i am autofelix</b></p>
<p class="nickname">飞兔小哥</p>
<a href="https://autofelix.blog.csdn.net" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="csdn">csdn主页</a>
<a href="https://xie.infoq.cn/u/autofelix/publish" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="infoq">infoq主页</a>
<a href="https://blog.51cto.com/autofelix" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="51cto">51cto主页</a>
<p class="attention">跪求关注 一键三连</p>
<p class="introduce">
<a href="https://www.cnblogs.com/autofelix" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="cnblogs">博客园主页</a>
</p>
</body>
</html>
"""
from bs4 import BeautifulSoup
soup = BeautifulSoup(html, 'html.parser')
body_tag=soup.body
print(body_tag)
# 以列表的形式输出,所有子节点
print(body_tag.contents)
# children 用来遍历子节点
for child in body_tag.children:
print(child)
七、find_all方法
是解析 HTML 文档的常用方法
find_all() 方法用来搜索当前 tag 的所有子节点
并判断这些节点是否符合过滤条件
最后以列表形式将符合条件的内容返回
html = """
<html>
<head>
<title>零基础学编程</title>
</head>
<body>
<p class="intro"><b>i am autofelix</b></p>
<p class="nickname">飞兔小哥</p>
<a href="https://autofelix.blog.csdn.net" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="csdn">csdn主页</a>
<a href="https://xie.infoq.cn/u/autofelix/publish" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="infoq">infoq主页</a>
<a href="https://blog.51cto.com/autofelix" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="51cto">51cto主页</a>
<p class="attention">跪求关注 一键三连</p>
<p class="introduce">
<a href="https://www.cnblogs.com/autofelix" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="cnblogs">博客园主页</a>
</p>
</body>
</html>
"""
import re
from bs4 import BeautifulSoup
# 创建soup解析对象
soup = BeautifulSoup(html, 'html.parser')
# 查找所有a标签并返回
print(soup.find_all("a"))
# 查找前两条a标签并返回,只返回两条a标签
print(soup.find_all("a",limit=2))
# 按照标签属性以及属性值查找
print(soup.find_all("p",class_="nickname"))
print(soup.find_all(id="infoq"))
# 列表行书查找tag标签
print(soup.find_all(['b','a']))
# 正则表达式匹配id属性值
print(soup.find_all('a',id=re.compile(r'.\d')))
print(soup.find_all(id=True))
# True可以匹配任何值,下面代码会查找所有tag,并返回相应的tag名称
for tag in soup.find_all(True):
print(tag.name,end=" ")
# 输出所有以b开始的tag标签
for tag in soup.find_all(re.compile("^b")):
print(tag.name)
# 简化前写法
soup.find_all("a")
# 简化后写法
soup("a")
八、find方法
html = """
<html>
<head>
<title>零基础学编程</title>
</head>
<body>
<p class="intro"><b>i am autofelix</b></p>
<p class="nickname">飞兔小哥</p>
<a href="https://autofelix.blog.csdn.net" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="csdn">csdn主页</a>
<a href="https://xie.infoq.cn/u/autofelix/publish" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="infoq">infoq主页</a>
<a href="https://blog.51cto.com/autofelix" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="51cto">51cto主页</a>
<p class="attention">跪求关注 一键三连</p>
<p class="introduce">
<a href="https://www.cnblogs.com/autofelix" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="cnblogs">博客园主页</a>
</p>
</body>
</html>
"""
import re
from bs4 import BeautifulSoup
# 创建soup解析对象
soup = BeautifulSoup(html, 'html.parser')
# 查找第一个a并直接返回结果
print(soup.find('a'))
# 查找title
print(soup.find('intro'))
# 匹配指定href属性的a标签
print(soup.find('a',href='https://autofelix.blog.csdn.net'))
# 根据属性值正则匹配
print(soup.find(class_=re.compile('tro')))
# attrs参数值
print(soup.find(attrs={'class': 'introduce'}))
# 使用 find 时,如果没有找到查询标签会返回 None,而 find_all 方法返回空列表
print(soup.find('aa'))
print(soup.find_all('bb'))
# 简化写法
print(soup.head.title)
# 上面代码等价于
print(soup.find("head").find("title"))
来源:https://blog.51cto.com/autofelix/5248473
0
投稿
猜你喜欢
- 1. viper的介绍viper是go一个强大的流行的配置解决方案的库。viper是spf13的另外一个重量级库。有大量项目都使用该库,比如
- 最近一直在做Dnn模块的开发,过程中碰到这么一个问题,需要同时插入N条数据,不想在程序里控制,但是SQL Sever又不支持数组参数.所以只
- 快速掌握 Mysql数据库对文件操作的封装在查看Mysql对文件的操作中,它在不同的操作系统上对文件的操作,除了使用标准C运行库函数,包括o
- 概要介绍mmpi,是一款使用python实现的开源邮件快速检测工具库,基于community框架设计开发。mmpi支持对邮件头、邮件正文、邮
- django在使用外键ForeignKey的时候,会自动给当前字段后面添加一个后缀_id。正常来说这样并不会影响使用。除非你要写原生sql,
- argparse 是python自带的命令行参数解析包,可以用来方便地读取命令行参数。一、传入一个参数import argpars
- 最近项目很忙没机会更新博客。有朋友在和我谈学艺术的就业问题,就随便谈一下自己的想法。每年中国美院报考的人数与日俱增,越来越多的人投入到艺术设
- pycharm指定python路径,pycharm配置python环境的方法是:1、依次点击【File】、【Project Interpre
- 今天在使用Pycharm的时候,由于文件过多,我对目录下的文件做了归类,改动了一些文件的路径,结果后来执行的时候,出现了路径找不到的错误.新
- 所谓定时器,是指间隔特定时间执行特定任务的机制。几乎所有的编程语言,都有定时器的实现。比如,Java有util.Timer和util.Tim
- 本文实例讲述了python通过pil模块将raw图片转换成png图片的方法。分享给大家供大家参考。具体分析如下:python通过pil模块将
- 面部识别----考勤打卡、注册登录、面部支付等等...感觉很高大上,又很方便,下面用python中的框架--django完成一个注册登录的功
- asp定时生成静态HTML的代码,对于缓解服务器压力有很大帮主,需要的朋友可以参考下。<% '判断是否要生成新的HT
- 近来想要做一做人脸识别相关的内容,主要是想集成一个系统,看到opencv已经集成了三种性能较好的算法,但是还是想自己动手试一下,毕竟算法都比
- 一、在工程目录中新建一个excel文件二、使用python脚本程序将目标excel文件中的列头写入,本文省略该部分的code展示,可自行网上
- 本文实例讲述了Django框架实现分页显示内容的方法。分享给大家供大家参考,具体如下:分页1、作用数据加载优化2、前端引入bootstrap
- 文件拆分代码:#-*-encoding:utf-8-*-import osimport sysimport threadingdef get
- 题目:解法一:比较元素是否相等思路说明:这种应该是普通人最先想到的解法,先获取到数组之后进行有小到大排序,然后初始化一个min=0(代表新数
- 导言作为web开发人员,我们的生活围绕着数据操作。我们建立数据库来存储数据,写编码来访问和修改数据,设计网页来采集和汇总数据。本文是研究在A
- 本篇阅读的代码片段来自于30-seconds-of-python(https://github.com/30-seconds...)。1.