经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » Python » 查看文章
python爬虫beautifulsoup库使用操作教程全解(python爬虫基础入门)
来源:jb51  时间:2021/2/19 13:44:06  对本文有异议

【python爬虫基础入门】系列是对python爬虫的一个入门练习实践,旨在用最浅显易懂的语言,总结最明了,最适合自己的方法,本人一直坚信,总结才会使人提高

1. BeautifulSoup库简介

BeautifulSoup库在python中被美其名为“靓汤”,它和和 lxml 一样也是一个HTML/XML的解析器,主要的功能也是如何解析和提取 HTML/XML 数据。BeautifulSoup支持Python标准库中的HTML解析器,还支持一些第三方的解析器,若在没用安装此库的情况下, Python 会使用 Python默认的解析器lxml,lxml 解析器更加强大,速度更快,而BeautifulSoup库中的lxml解析器则是集成了单独的lxml的特点,使得功能更加强大。

需要注意的是,Beautiful Soup已经自动将输入文档转换为Unicode编码,输出文档转换为utf-8编码。因此在使用它的时候不需要考虑编码方式,仅仅需要说明一下原始编码方式就可以了。

使用pip命令工具安装BeautifulSoup4库

  1. pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ BeautifulSoup # 使用清华大学镜像源安装

2. BeautifulSoup库的主要解析器

在代码中 html.parser是一种针对于html网页页面的解析器,Beautiful Soup库还有其他的解析器,用于针对不同的网页

  1. demo = 'https://www.baidu.com'
  2. soup = BeautifulSoup(demo,'html.parser')

解析器 使用方法 条件
bs4的html解析器 BeautifulSoup(demo,‘html.parser') 安装bs4库
lxml的html解析器 BeautifulSoup(demo,‘lxml') pip install lxml
lxml的xml解析器 BeautifulSoup(demo,‘xml') pip install lxml
html5lib的解析器 BeautifulSoup(demo,‘html5lib') pip install html5lib

3. BeautifulSoup的简单使用

假如有一个简单的网页,提取百度搜索页面的一部分源代码为例

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta content="text/html;charset=utf-8" http-equiv="content-type" />
  5. <meta content="IE=Edge" http-equiv="X-UA-Compatible" />
  6. <meta content="always" name="referrer" />
  7. <link
  8. href="https://ss1.bdstatic.com/5eN1bjq8AAUYm2zgoY3K/r/www/cache/bdorz/baidu.min.
  9. css" rel="stylesheet" type="text/css" />
  10. <title>百度一下,你就知道 </title>
  11. </head>
  12. <body link="#0000cc">
  13. <div >
  14. <div >
  15. <div >
  16. <div >
  17. <a href="http://news.baidu.com" rel="external nofollow" rel="external nofollow" name="tj_trnews">新闻
  18. </a>
  19. <a href="https://www.hao123.com" rel="external nofollow"
  20. name="tj_trhao123">hao123 </a>
  21. <a href="http://map.baidu.com" rel="external nofollow" name="tj_trmap">地图 </a>
  22. <a href="http://v.baidu.com" rel="external nofollow" name="tj_trvideo">视频 </a>
  23. <a href="http://tieba.baidu.com" rel="external nofollow" name="tj_trtieba">贴吧
  24. </a>
  25. <a href="//www.baidu.com/more/" rel="external nofollow" name="tj_briicon"
  26. >更多产品 </a>
  27. </div>
  28. </div>
  29. </div>
  30. </div>
  31. </body>
  32. </html>

结合requests库和使用BeautifulSoup库的html解析器,对其进行解析有如下

  1. import requests
  2. from bs4 import BeautifulSoup
  3.  
  4. # 使用Requests库加载页面代码
  5. r = requests.get('https://www.baidu.com')
  6. r.raise_for_status() # 状态码返回
  7. r.encoding = r.apparent_encoding
  8. demo = r.text
  9.  
  10. # 使用BeautifulSoup库解析代码
  11. soup = BeautifulSoup(demo,'html.parser') # 使用html的解析器
  12.  
  13. print(soup.prettify()) # prettify 方式输出页面

在这里插入图片描述

4. BeautifuSoup的类的基本元素

BeautifulSoup4将复杂HTML文档转换成一个复杂的树形结构,每个节点都是Python对象,BeautifulSoup库有针对于html的标签数的特定元素,重点有如下三种

  1. <p > ... </p>
  • Tag
  • NavigableString
  • Comment
  • BeautifulSoup

基本元素 说明
Tag 标签,最基本的信息组织单元,分别用<>和</>标明开头和结尾,格式:soup.a或者soup.p(获取a标签中或者p标签中的内容)
Name 标签的名字,

的名字是‘p' 格式为:.name
Attributes 标签的属性,字典形式组织,格式:.attrs
NavigableString 标签内非属性字符串,<>…</>中的字符串,格式:.string
Comment 标签内的字符串的注释部分,一种特殊的Comment类型

4.1 Tag

标签是html中的最基本的信息组织单元,使用方式如下

  1. from bs4 import BeautifulSoup
  2. html = 'https://www.baidu.com'
  3. bs = BeautifulSoup(html,"html.parser")
  4.  
  5. print(bs.title) # 获取title标签的所有内容
  6. print(bs.head) # 获取head标签的所有内容
  7. print(bs.a) # 获取第一个a标签的所有内容
  8. print(type(bs.a)) # 类型

在Tag标签中最重要的就是html页面中的name哈attrs属性,使用方式如下

  1. print(bs.name)
  2. print(bs.head.name) # head 之外对于其他内部标签,输出的值便为标签本身的名称
  3. print(bs.a.attrs) # 把 a 标签的所有属性打印输出了出来,得到的类型是一个字典。
  4. print(bs.a['class']) # 等价 bs.a.get('class') 也可以使用get方法,传入属性的名称,二者是等价的
  5. bs.a['class'] = "newClass" # 对这些属性和内容进行修改
  6. print(bs.a)
  7. del bs.a['class'] # 对这个属性进行删除
  8. print(bs.a)

4.2 NavigableString

NavigableString中的string方法用于获取标签内部的文字

  1. from bs4 import BeautifulSoup
  2. html = 'https://www.baidu.com'
  3. bs = BeautifulSoup(html,"html.parser")
  4. print(bs.title.string)
  5. print(type(bs.title.string))

4.3 Comment

Comment 对象是一个特殊类型的 NavigableString 对象,其输出的内容不包括注释符号,用于输出注释中的内容

  1. from bs4 import BeautifulSoup
  2. html = 'https://www.baidu.com'
  3. bs = BeautifulSoup(html,"html.parser")
  4. print(bs.a)
  5. # 标签中的内容<a href="http://news.baidu.com" rel="external nofollow" rel="external nofollow" name="tj_trnews"><!--新闻--></a>
  6. print(bs.a.string) # 新闻
  7. print(type(bs.a.string)) # <class 'bs4.element.Comment'>

5. 基于bs4库的HTML内容的遍历方法

在HTML中有如下特定的基本格式,也是构成HTML页面的基本组成成分

在这里插入图片描述

而在这种基本的格式下有三种基本的遍历流程

  • 下行遍历
  • 上行遍历
  • 平行遍历

三种种遍历方式分别是从当前节点出发。对之上或者之下或者平行的格式以及关系进行遍历

5.1 下行遍历

下行遍历有三种遍历的属性,分别是

  • contents
  • children
  • descendants

属性 说明
.contents 子节点的列表,将所有儿子节点存入列表
.children 子节点的迭代类型,用于循环遍历儿子节点
.descendants 子孙节点的迭代类型,包含所有子孙节点,用于循环遍历

使用举例

  1. soup = BeautifulSoup(demo,'html.parser')
  2.  
  3. # 循环遍历儿子节点
  4. for child in soup.body.children:
  5. print(child)
  6.  
  7. # 循环遍历子孙节点
  8. for child in soup.body.descendants:
  9. print(child)
  10. # 输出子节点的列表形式
  11. print(soup.head.contents)
  12. print(soup.head.contents[1]) # 用列表索引来获取它的某一个元素

5.2 上行遍历

上行遍历有两种方式

  • parent
  • parents

属性 说明
.parent 节点的父亲标签
.parents 节点先辈标签的迭代类型,用于循环遍历先辈节点,返回一个生成器
使用举例

  1. soup = BeautifulSoup(demo,'html.parser')
  2.  
  3. for parent in soup.a.parents:
  4. if parent is None:
  5. parent(parent)
  6. else:
  7. print(parent.name)

5.3 平行遍历

平行遍历有四种属性

  • next_sibling
  • previous_sibling
  • next_siblings
  • previous_siblings

属性 说明
.next_sibling 返回按照HTML文本顺序的下一个平行节点标签
.previous_sibling 返回按照HTML文本顺序的上一个平行节点标签
.next_siblings 迭代类型,返回按照html文本顺序的后续所有平行节点标签
.previous_siblings 迭代类型,返回按照html文本顺序的前序所有平行节点标签

在这里插入图片描述

平行遍历举例如下

  1. for sibling in soup.a.next_sibling:
  2. print(sibling) # 遍历后续节点
  3. for sibling in soup.a.previous_sibling:
  4. print(sibling) # 遍历

5.4 其他遍历

属性 说明
.strings 如果Tag包含多个字符串,即在子孙节点中有内容,可以用此获取,而后进行遍历
.stripped_strings 与strings用法一致,可以去除掉那些多余的空白内容
.has_attr 判断Tag是否包含属性

6. 文件树搜索

使用soup.find_all(name,attrs,recursive,string,**kwargs)方法,用于返回一个列表类型,存储查找的结果

  • name:对标签名称的检索字符串
  • attrs:对标签属性值得检索字符串,可标注属性检索
  • recursive:是否对子孙全部检索,默认为
  • Truestring:用与在信息文本中特定字符串的检索

6.1 name参数

如果是指定的字符串:会查找与字符串完全匹配的内容,如下

  1. a_list = bs.find_all("a")
  2. print(a_list) # 将会返回所有包含a标签的内容

如果是使用正则表达式:将会使用BeautifulSoup4中的search()方法来匹配内容,如下

  1. from bs4 import BeautifulSoup
  2. import re
  3.  
  4. html = 'https://www.baidu.com'
  5. bs = BeautifulSoup(html,"html.parser")
  6. t_list = bs.find_all(re.compile("a"))
  7. for item in t_list:
  8. print(item) # 输出列表

如果传入一个列表:BeautifulSoup4将会与列表中的任一元素匹配到的节点返回,如下

  1. t_list = bs.find_all(["meta","link"])
  2. for item in t_list:
  3. print(item)

如果传入一个函数或者方法:将会根据函数或者方法来匹配

  1. from bs4 import BeautifulSoup
  2.  
  3. html = 'https://www.baidu.com'
  4. bs = BeautifulSoup(html,"html.parser")
  5. def name_is_exists(tag):
  6. return tag.has_attr("name")
  7. t_list = bs.find_all(name_is_exists)
  8. for item in t_list:
  9. print(item)

6.2 attrs参数

并不是所有的属性都可以使用上面这种方式进行搜索,比如HTML的data属性,用于指定属性搜索

  1. t_list = bs.find_all(data-foo="value")

6.3 string参数

通过通过string参数可以搜索文档中的字符串内容,与name参数的可选值一样,string参数接受字符串,正则表达式,列表

  1. from bs4 import BeautifulSoup
  2. import re
  3.  
  4. html = 'https://www.baidu.com'
  5. bs = BeautifulSoup(html, "html.parser")
  6. t_list = bs.find_all(attrs={"data-foo": "value"})
  7. for item in t_list:
  8. print(item)
  9. t_list = bs.find_all(text="hao123")
  10. for item in t_list:
  11. print(item)
  12. t_list = bs.find_all(text=["hao123", "地图", "贴吧"])
  13. for item in t_list:
  14. print(item)
  15. t_list = bs.find_all(text=re.compile("\d"))
  16. for item in t_list:
  17. print(item)

使用find_all()方法的时,常用到正则表达式的形式import re如下所示

  1. soup.find_all(sring = re.compile('pyhton')) # 指定查找内容
  2.  
  3. # 或者指定使用正则表达式要搜索的内容
  4. sring = re.compile('pyhton') # 字符为python
  5. soup.find_all(string) # 调用方法模板

6.4 常用的fiid()方法如下

在这里插入图片描述

7. 总结参考

此文列举了BeautifulSoup库在爬虫中的基本使用,不正确之处望指教,参考

到此这篇关于python爬虫beautifulsoup库使用操作教程全解(python爬虫基础入门)的文章就介绍到这了,更多相关python爬虫beautifulsoup库内容请搜索w3xue以前的文章或继续浏览下面的相关文章希望大家以后多多支持w3xue!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号