爬虫解析库beautifulsoup
一、介绍
Beautiful Soup是一个可以从HTML或XML文件中提取数据的python库。
#安装Beautiful Soup
pip install beautifulsoup4 #安装解析器
Beatiful Soup支持python标准库中的HTML解析器,还支持一些第三方的解析器,其中一个是lxml,安装lxml:
pip install lxml #这个使用率最高的 另外一个可供选择的解析器是纯python实现的html5lib,html5lib的解析方式与浏览器相同,安装方式:
pip install html5lib
下表列出了主要的解析器,以及它们的优缺点,官网推荐使用lxml作为解析器,因为效率更高. 在Python2.7.3之前的版本和Python3中3.2.2之前的版本,必须安装lxml或html5lib, 因为那些Python版本的标准库中内置的HTML解析方法不够稳定.
| 解析器 | 使用方法 | 优势 | 劣势 |
|---|---|---|---|
| Python标准库 | BeautifulSoup(markup, "html.parser") |
|
|
| lxml HTML 解析器 | BeautifulSoup(markup, "lxml") |
|
|
| lxml XML 解析器 |
BeautifulSoup(markup, ["lxml", "xml"]) BeautifulSoup(markup, "xml") |
|
|
| html5lib | BeautifulSoup(markup, "html5lib") |
|
|
二、基本使用
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>
""" #基本使用:容错处理,文档的容错能力指的是在html代码不完整的情况下,使用该模块可以识别该错误。使用BeautifulSoup解析上述代码,能够得到一个 BeautifulSoup 的对象,并能按照标准的缩进格式的结构输出
from bs4 import BeautifulSoup
soup=BeautifulSoup(html_doc,'lxml') #具有容错功能
res=soup.prettify() #处理好缩进,结构化显示
print(res)
三、遍历文档树
遍历文档树:即直接通过标签名字选择,特点是选择速度快,但如果存在多个相同的标签只返回第一个
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>
""" from bs4 import BeautifulSoup
soup=BeautifulSoup(html_doc,'lxml') #具有容错功能
res=soup.prettify() #处理好缩进,结构化显示 #1.用法
print(soup.p) #存在多个相同的标签只取出第一个p标签
#<p class="title"><b>The Dormouse's story</b></p>
#2.获取标签的名称
print(soup.p.name) #p #3.获取标签的属性
print(soup.p.attrs) #{'class': ['title']} 以字典形式打印 #4.获取标签的内容
print(soup.p.string) #The Dormouse's story #p下的文本只有一个时才能取到,否则为None
print(soup.p.strings) #拿到的是生成器对象,取到p下所有的文本内容
print(soup.p.text) #取到p下所有的文本内容
for line in soup.stripped_strings: #去掉空白
print(line)
#5.子节点、子孙节点
print(soup.p.contents) #p下所有子节点
#[<b>The Dormouse's story</b>] print(soup.p.children) #得到一个迭代器,包含p下所有的子节点 <list_iterator object at 0x00000054B207C780>
for i,child in enumerate(soup.p.children): #循环取出
print(i,child) #0 <b>The Dormouse's story</b> print(soup.p.descendants) #获取p下面所有的子孙节点 <generator object descendants at 0x00000054B203C258>
for i,child in enumerate(soup.p.descendants): #文本也会显示出来
print(i,child) #0 <b>The Dormouse's story</b> 1 The Dormouse's story #6.父节点、祖先节点
print(soup.a.parent) #获取a标签的父节点
print(soup.a.parents) #找到a标签所有的祖父节点,父亲的父亲。。。
四、搜索文档树
1.五种过滤器
搜索文档树:Beautiful Soup定义了很多搜索方法,这里着重介绍2个:find()和find_all()
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>
""" from bs4 import BeautifulSoup
soup=BeautifulSoup(html_doc,'lxml')
res=soup.prettify() #处理好缩进,结构化显示 #1.五种过滤器:字符串、正则、列表、True、方法
#1.1 字符串:即标签名
print(soup.find_all('a')) #写标签名,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>] #1.2 正则
import re
print(soup.find_all(re.compile('^b'))) #找到b开头的标签,结果有body和b标签 #1.3列表:如果传入列表参数,Beautiful Soup会将与列表中任一元素匹配的结果返回
print(soup.find_all(['a',['b']])) #包含a和b标签 [<b>The Dormouse's story</b>, <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>] #1.4 True:可以匹配任何值,下面代码查到所有的tag,但是不会返回字符串节点
print(soup.find_all(True)) #返回所有标签
for tag in soup.find_all(True):
print(tag.name) #返回所有节点:html/p/a/head... #1.5方法:如果没有合适的过滤器,可以自定义一个方法,方法接受一个元素参数,如果方法返回True表示当前元素匹配成功,如果没找到返回False
def has_class_but_no_id(tag):
return tag.has_attr('class') and not tag.attr('id') print(soup.find_all(has_class_but_no_id))
2.find_all(name,attrs,recursive,text,**kwargs) 查询所有
#2.1 name:搜索name参数的值可以是任一类型的过滤器:字符串、正则、列表、True、方法
import re
print(soup.find_all(name=re.compile('^b'))) #这里加不加name都可以 #2.2 keyword: key=value的形式,value可以是过滤器:字符串、正则、列表、True
print(soup.find_all(class_=re.compile('title'))) #[<p class="title"><b>The Dormouse's story</b></p>] 匹配class是title的标签,注意类用class_
print(soup.find_all(id=True)) #查找有id属性的标签 #2.3按照类名查找,注意关键字是class_,class_=value,value可以是五种选择器之一
print(soup.find_all('a',class_='sister')) #查找类为sister的a标签
print(soup.find_all('a',class_='sister sss')) #查找类为sister和sss的a标签 #2.4 attrs
print(soup.find_all('p',attrs={'class':'story'})) #查找class=story的p标签 #2.5 text 值可以是:字符串,列表,True,正则
print(soup.find_all(text='Elsie')) #Elsie 文本
print(soup.find_all('a',text='Elsie')) #获取Elsie文本的a标签 #2.6 limit参数:限制取值个数
print(soup.find_all('a',limit=2)) #取两条a标签 #2.7 recursive:
print(soup.find_all('a')) #取出a标签的
print(soup.find_all('a',recursive=False)) #取出a标签的子节点 []
3.find(name,attrs,recursive,text,**kwargs) 查询第一个符合条件的标签
#3.find(name,attrs,recursive,text,**kwargs)
find_all() 方法将返回文档中符合条件的所有tag,尽管有时候我们只想得到一个结果。比如文档中只有一个a标签,那么可以直接使用find()方法来查找。 唯一区别就是find_all()查询到的结果返回的是包含元素的列表,而find()方法直接返回结果。
find_all()没有找到符合元素返回空列表,find()方法找不到目标时返回None.
4.css选择器 (******)使用select获取
#使用select方法来查询 #1.css选择器
print(soup.select('.sister')) #查询class=sister的标签 print(soup.select('#link1'))
print(soup.select('#link1')[0])
print(soup.select('#link1')[0].text) #只要下面可以取值就可以一直. 下去 #2.获取属性
print(soup.select('#link2')[0].attrs) #{'href': 'http://example.com/lacie', 'class': ['sister'], 'id': 'link2'} #3.获取内容
print(soup.select('#link1')[0].get_text())或者
print(soup.select('#link1')[0].text)
总结:
1.推荐使用lxml解析库
2.讲了三种选择器:标签选择器,find和find_all,css选择器
1.标签选择器筛选功能弱,但是速度快
2.建议使用find,find_all查询匹配单个结果或者多个结果
3.如果对css选择器非常熟悉建议使用select 3.获取属性attrs和获取文本get_text()方法
爬虫解析库beautifulsoup的更多相关文章
- 爬虫解析库——BeautifulSoup
解析库就是在爬虫时自己制定一个规则,帮助我们抓取想要的内容时用的.常用的解析库有re模块的正则.beautifulsoup.pyquery等等.正则完全可以帮我们匹配到我们想要住区的内容,但正则比较麻 ...
- 爬虫----爬虫解析库Beautifulsoup模块
一:介绍 Beautiful Soup 是一个可以从HTML或XML文件中提取数据的Python库.它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文档的方式.Beautiful Soup会帮你 ...
- 爬虫解析库BeautifulSoup的一些笔记
BeautifulSoup类使用 基本元素 说明 Tag 标签,最基本的信息组织单元,分别是<>和</>标明开头和结尾 Name 标签的名字,<p></p ...
- 【爬虫入门手记03】爬虫解析利器beautifulSoup模块的基本应用
[爬虫入门手记03]爬虫解析利器beautifulSoup模块的基本应用 1.引言 网络爬虫最终的目的就是过滤选取网络信息,因此最重要的就是解析器了,其性能的优劣直接决定这网络爬虫的速度和效率.Bea ...
- 【网络爬虫入门03】爬虫解析利器beautifulSoup模块的基本应用
[网络爬虫入门03]爬虫解析利器beautifulSoup模块的基本应用 1.引言 网络爬虫最终的目的就是过滤选取网络信息,因此最重要的就是解析器了,其性能的优劣直接决定这网络爬虫的速度和效率.B ...
- 爬虫 解析库re,Beautifulsoup,
re模块 点我回顾 Beautifulsoup模块 #安装 Beautiful Soup pip install beautifulsoup4 #安装解析器 Beautiful Soup支持Pytho ...
- python爬虫解析库之Beautifulsoup模块
一 介绍 Beautiful Soup 是一个可以从HTML或XML文件中提取数据的Python库.它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文档的方式.Beautiful Soup会 ...
- Python 爬虫 解析库的使用 --- Beautiful Soup
知道了正则表达式的相关用法,但是一旦正则表达式写的有问题,得到的可能就不是我们想要的结果了.而且对于一个网页来说,都有一定的特殊结构和层级关系,而且有很多节点都有id或class来做区分,所以借助它们 ...
- 爬虫 - 解析库之Beautiful Soup
了解Beautiful Soup 中文文档: Beautiful Soup 是一个可以从HTML或XML文件中提取数据的Python库.它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文档的方式 ...
随机推荐
- 洛谷P2029跳舞
题目 DP, 用的\(dp[i][j]\)表示\(i\)之前的数选了\(j\)个得到的最大结果,然后状态转移方程应该是 \[if (j \% t == 0)~~dp[i][j] = max(dp[i] ...
- coci2011 debt 还债
coci2011 debt 还债 Description 有N个人,每个人恰好欠另一个人Bi元钱,现在大家都没有钱,政府想要给其中一些人欠,使得大家都不欠别人钱. 如A欠B 50,B欠C 20,则当政 ...
- Android App专项测试
https://www.jianshu.com/p/141b84f14505 http://www.cnblogs.com/finer/p/9601140.html 专项 概念 adb命令 App启动 ...
- 【18NOIP普及组】对称二叉树(信息学奥赛一本通 1981)(洛谷 5018)
[题目描述] 一棵有点权的有根树如果满足以下条件,则被轩轩称为对称二叉树: 1.二叉树: 2.将这棵树所有节点的左右子树交换,新树和原树对应位置的结构相同且点权相等. 下图中节点内的数字为权值,节点外 ...
- git下载指定分支到本地
从网上查了很多方法有很多种,自我感觉下面这种更方便 git clone xxx.git --branch 分支名/dev/...
- 现有某电商网站用户对商品的收藏数据,记录了用户收藏的商品id以及收藏日期,名为buyer_favorite1。 buyer_favorite1包含:买家id,商品id,收藏日期这三个字段,数据以“\t”分割
实验内容(mapReduce安装请按照林子雨教程http://dblab.xmu.edu.cn/blog/631-2/) 现有某电商网站用户对商品的收藏数据,记录了用户收藏的商品id以及收藏日期,名为 ...
- Shiro安全框架-简介
1. 简介 Apache Shiro是Java的一个安全框架.功能强大,使用简单的Java安全框架,它为开发人员提供一个直观而全面的认证,授权,加密及会话管理的解决方案. 实际上,Shiro的主要功能 ...
- 帝国CMS万能标签标题截取后自动加入省略号
帝国CMS万能标签标题截取后自动加入省略号,没有达到字数的则不加省略号完美解决方案1.打开e/class/connect.php 搜索 if(!empty($subtitle))//截取字符 大约 ...
- 刷题之给定一个整数数组 nums 和一个目标值 taget,请你在该数组中找出和为目标值的那 两个 整数
今天下午,看了一会github,想刷个题呢,就翻出来了刷点题提高自己的实际中的解决问题的能力,在面试的过程中,我们发现,其实很多时候,面试官 给我们的题,其实也是有一定的随机性的,所以我们要多刷更多的 ...
- 作业——12 hadoop大作业
作业的要求来自于:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE2/homework/3339 Hadoop综合大作业 1.以下是爬虫大作业产生的csv文件 ...