山东省住房城乡建设厅网站,做布料的著名网站,中国建设工程监理协会官方网站,做推文网站除了秀米还要什么目录二十二、Python爬虫的BS4解析库22.1 BS4下载安装22.2 BS4解析对象22.3 BS4常用语法1) Tag节点22.4 遍历节点22.5 find_all()与find()1) find_all()2) find()22.6 CSS选择器二十二、Python爬虫的BS4解析库
Beautiful Soup 简称 BS4#xff08;其中 4 表示版本号#xff0…
目录二十二、Python爬虫的BS4解析库22.1 BS4下载安装22.2 BS4解析对象22.3 BS4常用语法1) Tag节点22.4 遍历节点22.5 find_all()与find()1) find_all()2) find()22.6 CSS选择器二十二、Python爬虫的BS4解析库
Beautiful Soup 简称 BS4其中 4 表示版本号是一个 Python 第三方库它可以从 HTML 或 XML 文档中快速地提取指定的数据。Beautiful Soup 语法简单使用方便并且容易理解因此您可以快速地学习并掌握它。
22.1 BS4下载安装
由于 Bautiful Soup 是第三方库因此需要单独下载下载方式非常简单执行以下命令即可安装
pip install bs4由于 BS4 解析页面时需要依赖文档解析器所以还需要安装 lxml 作为解析库
pip install lxmlPython 也自带了一个文档解析库 html.parser 但是其解析速度要稍慢于 lxml。除了上述解析器外还可以使用 html5lib解析器安装方式如下
pip install html5lib该解析器生成 HTML 格式的文档但速度较慢。
“解析器容错”指的是被解析的文档发生错误或不符合格式时通过解析器的容错性仍然可以按照既定的正确格式实现解析。
22.2 BS4解析对象
创建 BS4 解析对象是万事开头的第一步这非常地简单语法格式如下所示
#导入解析包
from bs4 import BeautifulSoup
#创建beautifulsoup解析对象
soup BeautifulSoup(html_doc, html.parser)上述代码中html_doc 表示要解析的文档而 html.parser 表示解析文档时所用的解析器此处的解析器也可以是 ‘lxml’ 或者’html5lib’示例代码如下所示
#coding:utf8
html_doc
htmlheadtitlec语言中文网/title/head
body
p classtitlebc.biancheng.net/b/p
p classwebsite一个学习编程的网站
a hrefhttp://c.biancheng.net/python/ idlink1python教程/a
a hrefhttp://c.biancheng.net/c/ idlink2c语言教程/afrom bs4 import BeautifulSoup
soup BeautifulSoup(html_doc, html.parser)
#prettify()用于格式化输出html/xml文档
print(soup.prettify())输出结果
html
headtitlec语言中文网/title
/head
bodyp classtitlebc.biancheng.net/b/pp classwebsite一个学习编程的网站a hrefhttp://c.biancheng.net/python/ idlink1python教程/aa hrefhttp://c.biancheng.net/c/ idlink2c语言教程/a
/body
/html如果是外部文档您也可以通过 open() 的方式打开读取语法格式如下
soup BeautifulSoup(open(html_doc.html, encodingutf8), lxml)22.3 BS4常用语法
下面对爬虫中经常用到的 BS4 解析方法做详细介绍。
Beautiful Soup 将 HTML 文档转换成一个树形结构该结构有利于快速地遍历和搜索 HTML 文档。下面使用树状结构来描述一段 HTML 文档
htmlheadtitlec语言中文网/title/headh1c.biancheng.net/h1pb一个学习编程的网站/b/p/body/html树状图如下所示 图1HTML文档树结构图 文档树中的每个节点都是 Python 对象这些对象大致分为四类Tag , NavigableString , BeautifulSoup ,Comment 。其中使用最多的是 Tag 和 NavigableString。
Tag标签类HTML 文档中所有的标签都可以看做 Tag 对象。NavigableString字符串类指的是标签中的文本内容使用 text、string、strings 来获取文本内容。BeautifulSoup表示一个 HTML 文档的全部内容您可以把它当作一个人特殊的 Tag 对象。Comment表示 HTML 文档中的注释内容以及特殊字符串它是一个特殊的 NavigableString。
1) Tag节点
标签Tag是组成 HTML 文档的基本元素。在 BS4 中通过标签名和标签属性可以提取出想要的内容。看一组简单的示例
from bs4 import BeautifulSoup
soup BeautifulSoup(p classWeb site urlbc.biancheng.net/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)输出结果如下
soup.p输出结果:
p classWeb site urlbc.biancheng.net/b/psoup.p.b输出结果
bc.biancheng.net/bsoup.p.text输出结果
c.biancheng.netsoup.p.attrs输出结果
{class: [Web, site, url]}type(soup.p)输出结果
class bs4.element.Tagsoup.p[class]输出结果
[Web, site, url]class属性重新赋值
p classWeb Sitebc.biancheng.net/b/p22.4 遍历节点
Tag 对象提供了许多遍历 tag 节点的属性比如 contents、children 用来遍历子节点parent 与 parents 用来遍历父节点而 next_sibling 与 previous_sibling 则用来遍历兄弟节点 。示例如下
#coding:utf8
from bs4 import BeautifulSouphtml_doc
htmlheadtitlec语言中文网/title/head
body
p classtitlebc.biancheng.net/b/p
p classwebsite一个学习编程的网站/p
a hrefhttp://c.biancheng.net/python/ idlink1python教程/a,
a hrefhttp://c.biancheng.net/c/ idlink2c语言教程/a andsoup BeautifulSoup(html_doc, html.parser)
body_tagsoup.body
print(body_tag)
#以列表的形式输出所有子节点
print(body_tag.contents)输出结果
body
p classtitlebc.biancheng.net/b/p
p classwebsite一个学习编程的网站/p
a hrefhttp://c.biancheng.net/python/ idlink1python教程/a,
a hrefhttp://c.biancheng.net/c/ idlink2c语言教程/a and
/body
#以列表的形式输出
[\n, p classtitlebc.biancheng.net/b/p, \n, p classwebsite一个学习编程的网站/p, \n, a hrefhttp://c.biancheng.net/python/ idlink1python教程/a, \n, a hrefhttp://c.biancheng.net/c/ idlink2c语言教程/a, \n]Tag 的 children 属性会生成一个可迭代对象可以用来遍历子节点示例如下
for child in body_tag.children:print(child)输出结果
#注意此处已将换行符\n省略
p classtitlebc.biancheng.net/b/p
p classwebsite一个学习编程的网站/p想了解更多相关示例可参考官方文档点击前往
22.5 find_all()与find()
find_all() 与 find() 是解析 HTML 文档的常用方法它们可以在 HTML 文档中按照一定的条件相当于过滤器查找所需内容。find() 与 find_all() 的语法格式相似希望大家在学习的时候可以举一反三。
BS4 库中定义了许多用于搜索的方法find() 与 find_all() 是最为关键的两个方法其余方法的参数和使用与其类似。
1) find_all()
find_all() 方法用来搜索当前 tag 的所有子节点并判断这些节点是否符合过滤条件最后以列表形式将符合条件的内容返回语法格式如下
find_all( name , attrs , recursive , text , limit )参数说明
name查找所有名字为 name 的 tag 标签字符串对象会被自动忽略。attrs按照属性名和属性值搜索 tag 标签注意由于 class 是 Python 的关键字吗所以要使用 “class_”。recursivefind_all() 会搜索 tag 的所有子孙节点设置 recursiveFalse 可以只搜索 tag 的直接子节点。text用来搜文档中的字符串内容该参数可以接受字符串 、正则表达式 、列表、True。limit由于 find_all() 会返回所有的搜索结果这样会影响执行效率通过 limit 参数可以限制返回结果的数量。
find_all() 使用示例如下
from bs4 import BeautifulSoup
import rehtml_doc
htmlheadtitlec语言中文网/title/head
body
p classtitlebc.biancheng.net/b/p
p classwebsite一个学习编程的网站/p
a hrefhttp://c.biancheng.net/python/ idlink1python教程/a
a hrefhttp://c.biancheng.net/c/ idlink2c语言教程/a
a hrefhttp://c.biancheng.net/django/ idlink3django教程/a
p classvip加入我们阅读所有教程/p
a hrefhttp://vip.biancheng.net/?fromindex idlink4成为vip/a#创建soup解析对象
soup BeautifulSoup(html_doc, html.parser)
#查找所有a标签并返回
print(soup.find_all(a))
#查找前两条a标签并返回
print(soup.find_all(a,limit2))
#只返回两条a标签
最后以列表的形式返回输出结果如下所示
[a hrefhttp://c.biancheng.net/python/ idlink1python教程/a, a hrefhttp://c.biancheng.net/c/ idlink2c语言教程/a, a hrefhttp://c.biancheng.net/django/ idlink3django教程/a, a hrefhttp://vip.biancheng.net/?fromindex idlink4成为vip/a][a hrefhttp://c.biancheng.net/python/ idlink1python教程/a, a hrefhttp://c.biancheng.net/c/ idlink2c语言教程/a]按照标签属性以及属性值查找 HTML 文档如下所示
print(soup.find_all(p,class_website))
print(soup.find_all(idlink4))输出结果
[p classwebsite一个学习编程的网站/p]
[a hrefhttp://vip.biancheng.net/?fromindex idlink4成为vip/a]正则表达式、列表以及 True 也可以当做过滤条件使用示例如下 #列表行书查找tag标签
print(soup.find_all([b,a]))
#正则表达式匹配id属性值
print(soup.find_all(a,idre.compile(r.\d)))
print(soup.find_all(idTrue))
#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)输出结果如下
第一个print输出
[bc.biancheng.net/b, a hrefhttp://c.biancheng.net/python/ idlink1python教程/a, a hrefhttp://c.biancheng.net/c/ idlink2c语言教程/a, a hrefhttp://c.biancheng.net/django/ idlink3django教程/a, a hrefhttp://vip.biancheng.net/?fromindex idlink4成为vip/a]
第二个print输出
[a hrefhttp://c.biancheng.net/python/ idlink1python教程/a, a hrefhttp://c.biancheng.net/c/ idlink2c语言教程/a, a hrefhttp://c.biancheng.net/django/ idlink3django教程/a, a hrefhttp://vip.biancheng.net/?fromindex idlink4成为vip/a]
第三个print输出
[a hrefhttp://c.biancheng.net/python/ idlink1python教程/a, a hrefhttp://c.biancheng.net/c/ idlink2c语言教程/a, a hrefhttp://c.biancheng.net/django/ idlink3django教程/a, a hrefhttp://vip.biancheng.net/?fromindex idlink4成为vip/a]
第四个print输出
html head title body p b p a a a p a
最后一个输出
body bBS4 为了简化代码为 find_all() 提供了一种简化写法如下所示
#简化前
soup.find_all(a)
#简化后
soup(a)上述两种的方法的输出结果是相同的。
2) find()
find() 方法与 find_all() 类似不同之处在于 find_all() 会将文档中所有符合条件的结果返回而 find() 仅返回一个符合条件的结果所以 find() 方法没有limit参数。使用示例如下
from bs4 import BeautifulSoup
import rehtml_doc
htmlheadtitlec语言中文网/title/head
body
p classtitlebc.biancheng.net/b/p
p classwebsite一个学习编程的网站/p
a hrefhttp://c.biancheng.net/python/ idlink1python教程/a
a hrefhttp://c.biancheng.net/c/ idlink2c语言教程/a
a hrefhttp://c.biancheng.net/django/ idlink3django教程/a
p classvip加入我们阅读所有教程/p
a hrefhttp://vip.biancheng.net/?fromindex idlink4成为vip/a#创建soup解析对象
soup BeautifulSoup(html_doc, html.parser)
#查找第一个a并直接返回结果
print(soup.find(a))
#查找title
print(soup.find(title))
#匹配指定href属性的a标签
print(soup.find(a,hrefhttp://c.biancheng.net/python/))
#根据属性值正则匹配
print(soup.find(class_re.compile(tit)))
#attrs参数值
print(soup.find(attrs{class:vip}))输出结果如下
a标签
a hrefhttp://c.biancheng.net/python/ idlink1python教程/a
指定href属性
a hrefhttp://c.biancheng.net/python/ idlink1python教程/a
title:
titlec语言中文网/title
正则匹配
p classtitlebc.biancheng.net/b/p
#attrs参数值
p classvip加入我们阅读所有教程/p使用 find() 时如果没有找到查询标签会返回 None而 find_all() 方法返回空列表。示例如下
print(soup.find(bdi))
print(soup.find_all(audio))输出结果如下
None
[]BS4 也为 find()提供了简化写法如下所示 #简化写法
print(soup.head.title)
#上面代码等价于
print(soup.find(head).find(title))两种写法的输出结果相同如下所示 titlec语言中文网/title
titlec语言中文网/title22.6 CSS选择器
BS4 支持大部分的 CSS 选择器比如常见的标签选择器、类选择器、id 选择器以及层级选择器。Beautiful Soup 提供了一个 select() 方法通过向该方法中添加选择器就可以在 HTML 文档中搜索到与之对应的内容。应用示例如下
#coding:utf8
html_doc
htmlheadtitlec语言中文网/title/head
body
p classtitlebc.biancheng.net/b/p
p classwebsite一个学习编程的网站/p
a hrefhttp://c.biancheng.net/python/ idlink1python教程/a
a hrefhttp://c.biancheng.net/c/ idlink2c语言教程/a
a hrefhttp://c.biancheng.net/django/ idlink3django教程/a
p classvip加入我们阅读所有教程/p
a hrefhttp://vip.biancheng.net/?fromindex idlink4成为vip/a
p classintroduce介绍:
a hrefhttp://c.biancheng.net/view/8066.html idlink5关于网站/a
a hrefhttp://c.biancheng.net/view/8092.html idlink6关于站长/a
/pfrom bs4 import BeautifulSoupsoup BeautifulSoup(html_doc, html.parser)
#根据元素标签查找
print(soup.select(title))
#根据属性选择器查找
print(soup.select(a[href]))
#根据类查找
print(soup.select(.vip))
#后代节点查找
print(soup.select(html head title))
#查找兄弟节点
print(soup.select(p a))
#根据id选择p标签的兄弟节点
print(soup.select(p ~ #link3))
#nth-of-type(n)选择器用于匹配同类型中的第n个同级兄弟元素
print(soup.select(p ~ a:nth-of-type(1)))
#查找子节点
print(soup.select(p a))
print(soup.select(.introduce #link5))输出结果
第一个输出
[titlec语言中文网/title]第二个输出
[a hrefhttp://c.biancheng.net/python/ idlink1python教程/a, a hrefhttp://c.biancheng.net/c/ idlink2c语言教程/a, a hrefhttp://c.biancheng.net/django/ idlink3django教程/a, a hrefhttp://vip.biancheng.net/?fromindex idlink4成为vip/a, a hrefhttp://c.biancheng.net/view/8066.html idlink5关于网站/a, a hrefhttp://c.biancheng.net/view/8092.html idlink6关于站长/a]第三个输出
[p classvip加入我们阅读所有教程/p]第四个输出
[titlec语言中文网/title]第五个输出
[a hrefhttp://c.biancheng.net/python/ idlink1python教程/a, a hrefhttp://vip.biancheng.net/?fromindex idlink4成为vip/a]第六个输出
[a hrefhttp://c.biancheng.net/django/ idlink3django教程/a]第七个输出
[a hrefhttp://c.biancheng.net/python/ idlink1python教程/a]第八个输出
[a hrefhttp://c.biancheng.net/view/8066.html idlink5关于网站/a, a hrefhttp://c.biancheng.net/view/8092.html idlink6关于站长/a]最后的print输出
[a hrefhttp://c.biancheng.net/view/8066.html idlink5关于网站/a]如果想了解更多关于 BS4 库的使用方法可以参考官方文档