BeautifulSoup4解析器
BeautifulSoup4解析器
BeautifulSoup4是一个HTML/XML的解析器,主要的功能是解析和提取HTML/XML的数据。和lxml库一样。
lxml只会局部遍历,而BeautifulSoup4是基于HTML DOM的,会加载整个文档,解析整个DOM树,因此内存开销比较大,性能比较低。
BeautifulSoup4用来解析HTML比较简单,API使用非常人性化,支持CSS选择器,是Python标准库中的HTML解析器,也支持lxml解析器。
BeautifulSoup4的安装
pip install beautifulsoup4
BeautifulSoup4的使用
以下面一段html文档为例子,如:
html_doc = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title"><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">Elsie</a>,
<a href="http://example.com/lacie" class="sister" 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>
"""
1. 使用BeautifulSoup类解析这段代码,获取一个BeautifulSoup的对象,然后按照标准格式输出。
from bs4 import BeautifulSoupsoup = BeautifulSoup(html_doc, 'html.parser')print(soup.prettify())
输出结果:
2. 几个简单的浏览结构化数据的方法
# 获取title标签
print(soup.title)
# <title>The Dormouse's story</title># 获取title标签名称
print(soup.title.name)
# title# 获取title标签的内容
print(soup.title.string)
# The Dormouse's story# 获取title的父标签
print(soup.title.parent)
# <head><title>The Dormouse's story</title></head># 获取title的父标签名称
print(soup.title.parent.name)
# head# 获取p标签
print(soup.p)
# <p class="title"><b>The Dormouse's story</b></p># 获取p标签class属性
print(soup.p['class'])
# ['title'] #返回的是list# 获取所有的a标签
print(soup.find_all('a'))
# [<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>, <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>, <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]# 获取id='link3'的标签
print(soup.find(id="link3"))
# <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a># 获取所有的a标签的链接
for link in soup.find_all('a'):print(link.get('href'))# http://example.com/elsie
# http://example.com/lacie
# http://example.com/tillie# 获取文档中所有文字内容
print(soup.get_text())# The Dormouse's story
#
# The Dormouse's story
# Once upon a time there were three little sisters; and their names were
# Elsie,
# Lacie and
# Tillie;
# and they lived at the bottom of a well.
# ...
3. BeautifulSoup的解析器
3.1 Python标准库
使用方法: BeautifulSoup(html_doc,"html.parser")优势:Python内置,执行速度适中,文档容错能力强劣势:Python 2.7.3 or 3.2.2)前 的版本中文档容错能力差
3.2 lxml解析器(推荐使用)
使用方法:BeautifulSoup(html_doc,'lxml')优势:速度快,文档容错能力强(C编写),推荐使用
3.3 html5lib
使用方法:BeautifulSoup(html_doc,"html5lib")优势:最好的容错性,已浏览器的方式解析文档,生成Html5格式的文档劣势:速度慢,不依赖外部扩展
4. 解析一个html文件
from bs4 import BeautifulSoup# 通过open返回一个文件对象,采用lxml解析,获取BeautifulSoup对象
soup = BeauifulSoup(open('index.html'),"lxml")
5. 对象的种类
BeautifulSoup将复杂HTML文档转换成一个复杂的属性结构,每个节点都是对象,所有对象分为4种类型:Tag
,NavigabString
,BeautifulSoup
,Comment
。
5.1 Tag:对象与XML或HTML原生文档中的tag相同
print(soup.title)
# <title>The Dormouse's story</title>print(type(soup.title))
# <class 'bs4.element.Tag'>
Tag有2个重要的属性:name , attrs
name:tag的标签名称
print(soup.title.name)
# title
attrs:tag的属性
print(soup.p.attrs)
# {'class': ['title']}
5.2 NatigabString:标签的文本内容
print(soup.p.string)
# The Dormouse's storyprint(type(soup.p.string))
# <class 'bs4.element.NavigableString'>
5.3 BeautifulSoup:表示一个文档内容,大部分时候,我们可以把它当做一个特殊的Tag
。
print(soup.name)
# [document]print(type(soup))
# <class 'bs4.BeautifulSoup'>
5.4 Comment:是一个特殊类型的 NavigableString 对象,其输出的内容不包括注释符号。
makeup='<p><!--Hello--></p>'
soup = BeatuifulSoup(makeup,'lxml')print(soup.p.string)
# Helloprint(type(soup.p.string))
# <class 'bs4.element.Comment'>
6. 遍历文档树
6.1 子节点:.contents
,.children
属性
tag的.contents
属性可以将tag的子节点以列表的方式输出:
print(soup.p.contents)
# [<b>The Dormouse's story</b>] # 因为只有一个节点print(soup.p.contents[0])
# [<b>The Dormouse's story</b>] 我们也可以获取列表的第一个标签。如果没有,会报错
tag的.children
返回一个生成器,可以对tag的子节点进行循环。
print(type(soup.p.children))
# <class 'list_iterator'>for child in soup.p.children:print(child) # <b>The Dormouse's story</b>
6.2 所有子孙节点.descendants
属性
.descendants
属性可以对所有的tag子孙节点进行递归循环,和.childern
类似。
for tag in soup.body.descendants:print(tag)# 输出结果:
# <b>The Dormouse's story</b>
# The Dormouse's story
7. 搜索文档树
7.1 find_all(name, attrs , recursive, text,limit, **kwargs)
find_all()参数:
name:查找名字为name的tag。(可以传入string,正则,列表)attrs:tag的属性recursive:是否递归,默认Truetext:tag标签文本limit:限制条数
7.1.1 name传入string
print(soup.find_all('p', attrs = {'class': 'title'}))
# [<p class="title"><b>The Dormouse's story</b></p>]print(soup.find_all('p', text='...'))
# [<p class="story">...</p>]print(soup.find_all('a', limit=2))
# [<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>, <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>]
7.1.2 name传入re正则表达式
for tag in soup.find_all(re.compile('^b')):print(tag.name)# body
# b
7.1.3 name传入列表
for tag in soup.find_all(['body','b']):print(tag.name)# body
# b
7.2 按CSS选择器搜索
7.2.1 通过标签名查找
print(soup.select('title'))
[<title>The Dormouse's story</title>]
7.2.2 通过类名查找
print(soup.select('.sister'))
# [<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>, <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>, <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]
7.2.3 通过id名查找
print(soup.select("#link1"))
# [<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>]
7.2.4 组合查找
print(soup.select('#link1,title'))
# [<title>The Dormouse's story</title>, <a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>]
7.2.5 属性查找
print(soup.select('a[class="sister"]'))
# [<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>, <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>, <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]
7.2.6 获取内容
for tag in soup.select('a'):print(tag.get_text())# Elsie
# Lacie
# Tillie
具体参考BeautifulSoup4官方文档
http://beautifulsoup.readthedocs.io/zh_CN/v4.4.0
BeautifulSoup4解析器相关推荐
- CSS 选择器:BeautifulSoup4解析器
和 lxml 一样,Beautiful Soup 也是一个HTML/XML的解析器,主要的功能也是如何解析和提取 HTML/XML 数据. lxml 只会局部遍历,而Beautiful Soup 是基 ...
- 非结构化数据与结构化数据提取---- BeautifulSoup4 解析器
CSS 选择器:BeautifulSoup4 和 lxml 一样,Beautiful Soup 也是一个HTML/XML的解析器,主要的功能也是如何解析和提取 HTML/XML 数据. lxml 只会 ...
- Python3.X 爬虫实战(静态下载器与解析器)
[工匠若水 http://blog.csdn.net/yanbober 未经允许严禁转载,请尊重作者劳动成果.私信联系我] 1 背景 这两天比较忙,各种锅锅接,忙里偷闲完结这一篇吧.在我们在上一篇&l ...
- python中的网页解析器_python爬虫初探(三):HTML解析器
爬虫初探系列一共4篇,耐心看完,我相信你就能基本了解爬虫是怎样工作的了,目录如下: 代码已上传至github,在python2.7下测试成功(请原谅我浓浓的乡村非主流代码风格)summerliehu/ ...
- Python爬虫(十二)_BeautifulSoup4 解析器
CSS选择器:BeautifulSoup4 和lxml一样,Beautiful Soup也是一个HTML/XML的解析器,主要的功能也是如何解析和提取HTML/XML数据. lxml只会局部遍历,而B ...
- python的网页解析器_python 之网页解析器
一.什么是网页解析器 1.网页解析器名词解释 首先让我们来了解下,什么是网页解析器,简单的说就是用来解析html网页的工具,准确的说:它是一个HTML网页信息提取工具,就是从html网页中解析提取出& ...
- python的网页解析器_网页解析器(BeautifulSoup)-- Python
分享一下关于 Python的网页解析器(BeautifulSoup) BeautifulSoup解析器 为了实现解析器,可以选择使用正则表达式.html.parser.BeautifulSoup.lx ...
- python自带网页解析器_python 之网页解析器
一.什么是网页解析器 1.网页解析器名词解释 首先让我们来了解下,什么是网页解析器,简单的说就是用来解析html网页的工具,准确的说:它是一个HTML网页信息提取工具,就是从html网页中解析提取出& ...
- Python爬虫之正则 BeautifulSoup4解析HTML
目录: 前言 编码演变历史 1 Charset Character Encoding 2 ASCII字符集与编码 3 中文字符集与编码 4 大一统的Unicode 5 UTF-8编码 6 Python ...
最新文章
- TensorFlow练习23: “恶作剧”
- mysql服务的基本操作_MySql(一)mysql服务的基本操作及环境配置
- mongodb学习篇
- js对文字批注_实现SpreadJS的自定制批注
- Linux运维系统工程师与java基础学习系列-6
- JSON模式在构建和部署API中的作用
- initial、inherit、unset、revert和all
- java 虚类private 继承_Java经典面试36题和答案
- Careercup - Microsoft面试题 - 5428361417457664
- 【docker】安装教程
- 非常全面的讲解SpringCloud中Zuul网关原理及其配置,看它就够了!
- 如何将微商管理模式流程化
- (八)ASP.NET自定义用户控件(1)
- 使用SonarQube Scanner for Maven 分析
- 1688商品类目API接口-(item_cat_get-获得1688商品类目接口)
- 9个免费的矢量图网站
- 实用插件(七)视频播放插件——ckplayer
- 【学习笔记】人工智能相关概念
- 道路交通车路协同信息服务通用技术要求
- C 语言有哪些优点?
热门文章
- python tkinter 安装_为Python安装tkinter
- 0X80000000与0X7FFFFFFF
- git 更新远程代码到本地仓库
- WordPress的have_posts()和the_post()用法解析
- Qt新手入门指南 - 如何创建模型/视图(二)
- 我最喜欢的互动媒体作业
- 基于matlab的双音多频信号识别,基于MATLAB的双音多频信号识别
- android 屏幕投射_将自定义内容从Android应用投射到电视(2020年方法)
- nodejs技术摘要
- MobPush Android For Unity