原文连接https://www.fkomm.cn/article/2018/7/20/18.htmlhtml
bs4库之因此能快速的定位咱们想要的元素,是由于他可以用一种方式将html文件解析了一遍 ,不一样的解析器有不一样的效果。下文将一一进行介绍。python
网络爬虫的最终目的就是过滤选取网络信息,最重要的部分能够说是解析器。解析器的优劣决定了爬虫的速度和效率。bs4库除了支持咱们上文用过的‘html.parser’解析器外,还支持不少第三方的解析器,下面咱们来对他们进行对比分析。windows
bs4库官方推荐咱们使用的是lxml解析器,缘由是它具备更高的效率,因此咱们也将采用lxml解析器。bash
$ pip install lxml
网络
注意,因为我用的是unix类系统,用pip工具十分的方便,可是若是在windows下安装,老是会出现这样或者那样的问题,这里推荐win用户去lxml官方,下载安装包,来安装适合本身系统版本的lxml解析器。工具
咱们依旧以上一篇的 爱丽丝文档 为例子:学习
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> """
复制代码
试一下吧:ui
import bs4
#首先咱们先将html文件已lxml的方式作成一锅汤
soup = bs4.BeautifulSoup(open('Beautiful Soup 爬虫/demo.html'),'lxml')
#咱们把结果输出一下,是一个很清晰的树形结构。
#print(soup.prettify())
''' OUT: <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 class="sister" href="http://example.com/elsie" id="link1"> Elsie </a> , <a class="sister" href="http://example.com/lacie" id="link2"> Lacie </a> and <a class="sister" href="http://example.com/tillie" id="link3"> Tillie </a> ; and they lived at the bottom of a well. </p> <p class="story"> ... </p> </body> </html> ··· 复制代码
bs4 库首先将传入的字符串或文件句柄转换为 Unicode的类型,这样,咱们在抓取中文信息的时候,就不会有很麻烦的编码问题了。固然,有一些生僻的编码 如:‘big5’,就须要咱们手动设置编码: soup = BeautifulSoup(markup, from_encoding="编码方式")编码
bs4 库将复杂的html文档转化为一个复杂的树形结构,每一个节点都是Python对象 ,全部对象能够分为如下四个类型:Tag , NavigableString , BeautifulSoup , Comment 咱们来逐一解释:spa
Tag: 和html中的Tag基本没有区别,能够简单上手使用
NavigableString: 被包裹在tag内的字符串
BeautifulSoup: 表示一个文档的所有内容,大部分的时候能够吧他看作一个tag对象,支持遍历文档树和搜索文档树方法。
Comment:这是一个特殊的NavigableSting对象,在出如今html文档中时,会以特殊的格式输出,好比注释类型。
soup.head
# <head><title>The Dormouse's story</title></head>
soup.title
# <title>The Dormouse's story</title>
复制代码
若是你还想更深刻的得到更小的tag:例如咱们想找到body下的被b标签包裹的部分
soup.body.b
# <b>The Dormouse's story</b>
复制代码
可是这个方法只能找到按顺序第一个出现的tag。
这个时候须要find_all()方法,他返回一个列表类型
tag=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>]
#假设咱们要找到a标签中的第二个元素:
need = tag[1]
#简单吧
复制代码
head_tag = soup.head
head_tag
# <head><title>The Dormouse's story</title></head>
head_tag.contents
[<title>The Dormouse's story</title>] title_tag = head_tag.contents[0] print(title_tag) # <title>The Dormouse's story</title>
title_tag.contents
# [u'The Dormouse's story']
复制代码
for child in title_tag.children:
print(child)
# The Dormouse's story
复制代码
for child in head_tag.descendants:
print(child)
# <title>The Dormouse's story</title>
# The Dormouse's story
复制代码
若是该tag只有一个子节点(NavigableString类型):直接使用tag.string就能找到。
若是tag有不少个子、孙节点,而且每一个节点里都string:
咱们能够用迭代的方式将其所有找出:
for string in soup.strings:
print(repr(string))
# u"The Dormouse's story"
# u'\n\n'
# u"The Dormouse's story"
# u'\n\n'
# u'Once upon a time there were three little sisters; and their names were\n'
# u'Elsie'
# u',\n'
# u'Lacie'
# u' and\n'
# u'Tillie'
# u';\nand they lived at the bottom of a well.'
# u'\n\n'
# u'...'
# u'\n'
复制代码
好了,关于bs4库的基本使用,咱们就先介绍到这。剩下来的部分: 父节点、兄弟节点、回退和前进,都与上面从子节点找元素的过程差很少。
圆方圆学院聚集 Python + AI 名师,打造精品的 Python + AI 技术课程。 在各大平台都长期有优质免费公开课,欢迎报名收看。
公开课地址:ke.qq.com/course/3627…
加入python学习讨论群 78486745 ,获取资料,和广大群友一块儿学习。