Python Beautiful Soup 解析库的使用
Beautiful Soup
借助网页的结构和属性等特性来解析网页,这样就可以省去复杂的正则表达式的编写。
Beautiful Soup是Python的一个HTML或XML的解析库。
1.解析器
解析器 | 使用方法 | 优势 | 劣势 |
Python标准库 | BeautifulSoup(markup,"html.parser") | 执行速度适中、文档容错能力强 | 2.7.3和3.2.2之前的版本容错能力差 |
lxml HTML解析器 | BeautifulSoup(markup,"lxml") | 速度快、文档容错能力强 | 需要安装C语言库 |
lxml XML解析器 | BeautifulSoup(markup,"xml") | 速度快,唯一支持XML的解析器 | 需要安装C语言库 |
html5lib | BeautifulSoup(markup,"html5lib") | 最好的容错性、以浏览器的方式解析文档、生成HTML5格式的文档 | 速度慢、不依赖外部扩展 |
综上所述,推荐lxml HTML解析器
1
2
3
|
from bs4 import BeautifulSoup soup = BeautifulSoup( '<p>Hello World</p>' , 'lxml' ) print (soup.p.string) |
2.基本用法:
1
2
3
4
5
6
7
8
9
10
11
|
html = ''' <html> <head><title>Infi-chu example</title></head> <body> <p class="title" name="dr"><b>title example</b></p> <p class="story">link <a href="http://example.com/elsie" class="sister" id="link1">elsie</a>, <a href="http://example.com/lacie" class="sister" id="link2">lacie</a>, <a href="http://example.com/tillie" class="sister" id="link3">tillie</a>, last sentence</p> ''' |
1
2
3
4
|
from bs4 import BeautifulSoup soup = BeautifulSoup(html, 'lxml' ) print (soup.prettify()) # 修复html print (soup.title.string) # 输出title节点的字符串内容 |
3.节点选择器:
选择元素
使用soup.元素的方式获取
提取信息
(1)获取名称
使用soup.元素.name获取元素名称
(2)获取属性
使用soup.元素.attrs
使用soup.元素.attrs['name']
(3)元素内容
使用soup.元素.string获取内容
嵌套选择
使用soup.父元素.元素.string获取内容
关联选择
(1)子节点和子孙节点
1
2
3
4
5
6
7
8
9
10
11
|
html = ''' <html> <head><title>Infi-chu example</title></head> <body> <p class="title" name="dr"><b>title example</b></p> <p class="story">link <a href="http://example.com/elsie" class="sister" id="link1"><span>elsie</span></a>, <a href="http://example.com/lacie" class="sister" id="link2"><span>lacie</span></a>, <a href="http://example.com/tillie" class="sister" id="link3"><span>tillie</span></a>, last sentence</p> ''' |
1
2
3
4
5
6
7
8
9
10
11
12
|
from bs4 import BeautifulSoup # 得到直接子节点,children属性 soup = BeautifulSoup(html, 'lxml' ) print (soup.p.children) for i ,child in enumerate (soup.p.children): print (i,child) # 得到所有的子孙节点,descendants属性 soup = BeautifulSoup(html, 'lxml' ) print (soup.p.descendants) for i,child in enmuerate(soup.p.descendants): print (i,child) |
(2)父节点和祖先节点
调用父节点,使用parent属性
获取所有祖先节点,使用parents属性
(3)兄弟节点
next_sibling 下一个兄弟元素
previous_sibling 上一个兄弟元素
next_siblings 所有前面兄弟节点
previous_siblings 所有后面兄弟节点
(4)提取信息
4.方法选择器:
find_all()
find_all(name,attrs,recursize,text,**kwargs)
(1)name
1
2
3
|
soup.find_all(name = 'ul' ) for ul in soup.find_all(name = 'ul' ): print (ul.find_all(name = 'ul' )) |
1
2
3
4
|
for ul in soup.find_all(name = 'ul' ): print (ul.find_all(name = 'li' )) for li in ul.find_all(name = 'li' ): print (li.string) |
(2)attes
1
2
3
4
5
6
7
|
# 根据节点名查询 print (soup.find_all(attrs = { 'id' : 'list1' })) print (soup.find_all(attrs = { 'name' : 'elements' })) # 也可以写成 print (soup.find_all( id = 'list1' )) print (soup.find_all( class = 'elements' )) |
(3)text
text参数可以用来匹配节点的文本,传入的形式可以是字符串,可以是正则表达式对象
1
2
3
|
from bs4 import BeautifulSoup soup = BeautifulSoup(html, 'lxml' ) print (soup.find_all(text = re. compile ( 'link' ))) |
find()
返回一个元素
【注】
find_parents()和find_parent()
find_next_siblings()和find_next_sibling()
find_previous_siblings()和find_previous_sibling()
find_all_next()和find_next()
find_all_previous()和find_previous()
5.CSS选择器:
嵌套选择
1
2
|
for ul in soup.select( 'ul' ): print (ul.select( 'li' )) |
获取属性
1
2
3
4
|
for ul in soup.select( 'ul' ): print (ul[ 'id' ]) # 等价于 print (ul.attrs[ 'id' ]) |
获取文本
获取文本除了string属性还有get_text()方法
1
2
3
4
|
for li in soup.select( 'li' ): # 效果一样 print (li.get_text()) print (li.string) |
Python Beautiful Soup 解析库的使用的更多相关文章
- Python爬虫之Beautiful Soup解析库的使用(五)
Python爬虫之Beautiful Soup解析库的使用 Beautiful Soup-介绍 Python第三方库,用于从HTML或XML中提取数据官方:http://www.crummv.com/ ...
- Beautiful Soup解析库的安装和使用
Beautiful Soup是Python的一个HTML或XML的解析库,我们可以用它来方便地从网页中提取数据.它拥有强大的API和多样的解析方式.官方文档:https://www.crummy.co ...
- 爬虫(五)—— 解析库(二)beautiful soup解析库
目录 解析库--beautiful soup 一.BeautifulSoup简介 二.安装模块 三.Beautiful Soup的基本使用 四.Beautiful Soup查找元素 1.查找文本.属性 ...
- (17)python Beautiful Soup 4.6
一.安装 1.登陆官网:https://www.crummy.com/software/BeautifulSoup/ 2.下载 3.解压 4.安装 cmd找到文件路径,运行 setup.py buil ...
- 爬虫5_python2_使用 Beautiful Soup 解析数据
使用 Beautiful Soup 解析数据(感谢东哥) 有的小伙伴们对写正则表达式的写法用得不熟练,没关系,我们还有一个更强大的工具,叫Beautiful Soup,有了它我们可以很方便地提取出HT ...
- 用Beautiful Soup解析html源码
#xiaodeng #python3 #用Beautiful Soup解析html源码 html_doc = """ <html> <head> ...
- python爬虫之解析库Beautiful Soup
为何要用Beautiful Soup Beautiful Soup是一个可以从HTML或XML文件中提取数据的Python库.它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文档的方式, 是一个 ...
- python beautiful soup库的超详细用法
原文地址https://blog.csdn.net/love666666shen/article/details/77512353 参考文章https://cuiqingcai.com/1319.ht ...
- Python爬虫系列(四):Beautiful Soup解析HTML之把HTML转成Python对象
在前几篇文章,我们学会了如何获取html文档内容,就是从url下载网页.今天开始,我们将讨论如何将html转成python对象,用python代码对文档进行分析. (牛小妹在学校折腾了好几天,也没把h ...
随机推荐
- iOS App多语言国际化
/*************************************1*******************************************/ /*************** ...
- CF 1093E Intersection of Permutations——CDQ分治
题目:http://codeforces.com/contest/1093/problem/E 只能想到转化成查询一个区间里值在一个范围里的数的个数…… 没有想到这样适合用主席树套树状数组维护.不过据 ...
- 基于Oracle的EntityFramework的WEBAPI2的实现(三)—— 建立APIController及设置返回类型JSON、XML等
建立普通的ApiControler 右击项目中的controller文件夹·添加·控制器·包含操作的webapi2控制器(使用entity framework),写个名字,如果:Test.然后选择类, ...
- STM32启动过程解读与跟踪验证
经过查阅各种官方文献和对代码进行单步跟踪,详细地叙述了STM32加电启动的具体过程.对于关键性的语句都指明了出处.下面将学习成果分享给大家,由于笔者知识有限,不当之处敬请指出. 为了更好的说明问题,先 ...
- html5 模块
1.<header> 网站头部标签2.<nav> 导航标签3.<article> 内容标签4.<section> 文章标签5.<aside> ...
- bootstrap的折叠组件1
官网的例子: http://v3.bootcss.com/javascript/#collapse <div class="panel-group" id="acc ...
- nginx限制请求之二:(ngx_http_limit_req_module)模块
相关文章: <高可用服务设计之二:Rate limiting 限流与降级> <nginx限制请求之一:(ngx_http_limit_conn_module)模块> <n ...
- MySQL添加数据库的唯一索引的几种方式~
创建表时直接设置: DROP TABLE IF EXISTS `student`;CREATE TABLE `student` ( `stu_id` int(11) NOT NULL AUTO_IN ...
- 查看Unix/Linux的CPU个数和内存大小,系统位数(转载)
一.AIX 1.查看CPU数: (1) smtctl 从AIX5.3起,对于power5的机器,系统引入了SMT(Simultaneousmulti-threading)的功能,其允许两个处理线程在同 ...
- 从一个开发的角度看负载均衡和LVS--FullNat
从一个开发的角度看负载均衡和LVS 在大规模互联网应用中,负载均衡设备是必不可少的一个节点,源于互联网应用的高并发和大流量的冲击压力,我们通常会在服务端部署多个无状态的应用服务器和若干有状态的存储服务 ...