Python爬虫BS4库的解析器正确使用方法

淩亂°似流年 2023-07-17 14:12 123阅读 0赞

bs4库之所以能快速的定位我们想要的元素,是因为他能够用一种方式将html文件解析了一遍 ,不同的解析器有不同的效果。下文将一一进行介绍。

bs4解析器的选择

网络爬虫的最终目的就是过滤选取网络信息,最重要的部分可以说是解析器。解析器的优劣决定了爬虫的速度和效率。bs4库除了支持我们上文用过的‘html.parser’解析器外,还支持很多第三方的解析器,下面我们来对他们进行对比分析。

bs4库官方推荐我们使用的是lxml解析器,原因是它具有更高的效率,所以我们也将采用lxml解析器。
PS注意:很多人学Python过程中会遇到各种烦恼问题,没有人解答容易放弃。为此小编建了个Python全栈免费答疑.裙 :七衣衣九七七巴而五(数字的谐音)转换下可以找到了,不懂的问题有老司机解决里面还有最新Python实战教程免非下,,一起相互监督共同进步!

lxml解析器的安装:

  • 依旧采用pip安装工具来安装:
  1. $ pip install lxml

注意,由于我用的是unix类系统,用pip工具十分的方便,但是如果在windows下安装,总是会出现这样或者那样的问题,这里推荐win用户去lxml官方,下载安装包,来安装适合自己系统版本的lxml解析器。

使用lxml解析器来解释网页

我们依旧以上一篇的 爱丽丝文档 为例子

  1. html_doc = """
  2. <html><head><title>The Dormouse's story</title></head>
  3. <body>
  4. <p class="title"><b>The Dormouse's story</b></p>
  5. <p class="story">Once upon a time there were three little sisters; and their names were
  6. <a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
  7. <a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
  8. <a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
  9. and they lived at the bottom of a well.</p>
  10. <p class="story">...</p>
  11. """

试一下吧:

  1. import bs4
  2. #首先我们先将html文件已lxml的方式做成一锅汤
  3. soup = bs4.BeautifulSoup(open('Beautiful Soup 爬虫/demo.html'),'lxml')
  4. #我们把结果输出一下,是一个很清晰的树形结构。
  5. #print(soup.prettify())
  6. '''
  7. OUT:
  8. <html>
  9. <head>
  10. <title>
  11. The Dormouse's story
  12. </title>
  13. </head>
  14. <body>
  15. <p class="title">
  16. <b>
  17. The Dormouse's story
  18. </b>
  19. </p>
  20. <p class="story">
  21. Once upon a time there were three little sisters; and their names were
  22. <a class="sister" href="http://example.com/elsie" id="link1">
  23. Elsie
  24. </a>
  25. ,
  26. <a class="sister" href="http://example.com/lacie" id="link2">
  27. Lacie
  28. </a>
  29. and
  30. <a class="sister" href="http://example.com/tillie" id="link3">
  31. Tillie
  32. </a>
  33. ;
  34. and they lived at the bottom of a well.
  35. </p>
  36. <p class="story">
  37. ...
  38. </p>
  39. </body>
  40. </html>
  41. '''

如何具体的使用?

bs4 库首先将传入的字符串或文件句柄转换为 Unicode的类型,这样,我们在抓取中文信息的时候,就不会有很麻烦的编码问题了。当然,有一些生僻的编码 如:‘big5’,就需要我们手动设置编码:
soup = BeautifulSoup(markup, from_encoding=”编码方式”)

对象的种类:

bs4 库将复杂的html文档转化为一个复杂的树形结构,每个节点都是Python对象 ,所有对象可以分为以下四个类型:Tag , NavigableString , BeautifulSoup , Comment
我们来逐一解释:

Tag: 和html中的Tag基本没有区别,可以简单上手使用

NavigableString: 被包裹在tag内的字符串

BeautifulSoup: 表示一个文档的全部内容,大部分的时候可以吧他看做一个tag对象,支持遍历文档树和搜索文档树方法。

Comment:这是一个特殊的NavigableSting对象,在出现在html文档中时,会以特殊的格式输出,比如注释类型。

搜索文档树的最简单的方法就是搜索你想获取tag的的name:

  1. soup.head
  2. # <head><title>The Dormouse's story</title></head>
  3. soup.title
  4. # <title>The Dormouse's story</title>

如果你还想更深入的获得更小的tag:例如我们想找到body下的被b标签包裹的部分

  1. soup.body.b
  2. # <b>The Dormouse's story</b>

但是这个方法只能找到按顺序第一个出现的tag

获取所有的标签呢?

这个时候需要find_all()方法,他返回一个列表类型

  1. tag=soup.find_all('a')
  2. # [<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>,
  3. # <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>,
  4. # <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]
  5. #假设我们要找到a标签中的第二个元素:
  6. need = tag[1]
  7. #简单吧

tag的.contents属性可以将tag的子节点以列表的方式输出:

  1. head_tag = soup.head
  2. head_tag
  3. # <head><title>The Dormouse's story</title></head>
  4. head_tag.contents
  5. [<title>The Dormouse's story</title>]
  6. title_tag = head_tag.contents[0]
  7. print(title_tag)
  8. # <title>The Dormouse's story</title>
  9. title_tag.contents
  10. # [u'The Dormouse's story']
  • 另外通过tag的 .children生成器,可以对tag的子节点进行循环:
  1. for child in title_tag.children:
  2. print(child)
  3. # The Dormouse's story
  • 这种方式只能遍历出子节点。如何遍历出子孙节点呢?

子孙节点:比如 head.contents 的子节点是The Dormouse’s story,这里 title本身也有子节点:‘The Dormouse‘s story’ 。这里的‘The Dormouse‘s story’也叫作head的子孙节点

  1. for child in head_tag.descendants:
  2. print(child)
  3. # <title>The Dormouse's story</title>
  4. # The Dormouse's story

如何找到tag下的所有的文本内容呢?

1、如果该tag只有一个子节点(NavigableString类型):直接使用tag.string就能找到。

2、如果tag有很多个子、孙节点,并且每个节点里都string:

我们可以用迭代的方式将其全部找出:

  1. for string in soup.strings:
  2. print(repr(string))
  3. # u"The Dormouse's story"
  4. # u'\n\n'
  5. # u"The Dormouse's story"
  6. # u'\n\n'
  7. # u'Once upon a time there were three little sisters; and their names were\n'
  8. # u'Elsie'
  9. # u',\n'
  10. # u'Lacie'
  11. # u' and\n'
  12. # u'Tillie'
  13. # u';\nand they lived at the bottom of a well.'
  14. # u'\n\n'
  15. # u'...'
  16. # u'\n'好了,关于bs4库的基本使用,我们就先介绍到这。剩下来的部分:父节点、兄弟节点、回退和前进,都与上面从子节点找元素的过程差不多,
  17. 总结很多人学Python过程中会遇到各种烦恼问题,没有人解答容易放弃。为此小编建了个Python全栈免费答疑.裙 :七衣衣九七七巴而五(数字的谐音)转换下可以找到了,不懂的问题有老司机解决里面还有最新Python实战教程免非下,,一起相互监督共同进步!本文的文字及图片来源于网络加上自己的想法,仅供学习、交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理。

发表评论

表情:
评论列表 (有 0 条评论,123人围观)

还没有评论,来说两句吧...

相关阅读