这篇教程python爬虫beautifulsoup库使用操作教程全解(python爬虫基础入门)写得很实用,希望能帮到您。 【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库 pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ BeautifulSoup # 使用清华大学镜像源安装
2. BeautifulSoup库的主要解析器在代码中 html.parser 是一种针对于html网页页面的解析器,Beautiful Soup库还有其他的解析器,用于针对不同的网页 demo = 'https://www.baidu.com'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的简单使用假如有一个简单的网页,提取百度搜索页面的一部分源代码为例 <!DOCTYPE html><html><head> <meta content="text/html;charset=utf-8" http-equiv="content-type" /> <meta content="IE=Edge" http-equiv="X-UA-Compatible" /> <meta content="always" name="referrer" /> <linkhref="https://ss1.bdstatic.com/5eN1bjq8AAUYm2zgoY3K/r/www/cache/bdorz/baidu.min.css" rel="stylesheet" type="text/css" /> <title>百度一下,你就知道 </title></head><body link="#0000cc"> <div > <div > <div > <div > <a href="http://news.baidu.com" rel="external nofollow" rel="external nofollow" name="tj_trnews">新闻</a> <a href="https://www.hao123.com" rel="external nofollow" name="tj_trhao123">hao123 </a> <a href="http://map.baidu.com" rel="external nofollow" name="tj_trmap">地图 </a> <a href="http://v.baidu.com" rel="external nofollow" name="tj_trvideo">视频 </a> <a href="http://tieba.baidu.com" rel="external nofollow" name="tj_trtieba">贴吧</a> <a href="//www.baidu.com/more/" rel="external nofollow" name="tj_briicon">更多产品 </a> </div> </div> </div> </div></body></html> 结合requests库和使用BeautifulSoup库的html解析器,对其进行解析有如下 import requestsfrom bs4 import BeautifulSoup# 使用Requests库加载页面代码r = requests.get('https://www.baidu.com')r.raise_for_status() # 状态码返回r.encoding = r.apparent_encodingdemo = r.text# 使用BeautifulSoup库解析代码soup = BeautifulSoup(demo,'html.parser') # 使用html的解析器print(soup.prettify()) # prettify 方式输出页面 
4. BeautifuSoup的类的基本元素BeautifulSoup4将复杂HTML文档转换成一个复杂的树形结构,每个节点都是Python对象,BeautifulSoup库有针对于html的标签数的特定元素,重点有如下三种 - Tag
- NavigableString
- Comment
- BeautifulSoup
基本元素 | 说明 | Tag | 标签,最基本的信息组织单元,分别用<>和</>标明开头和结尾,格式:soup.a或者soup.p(获取a标签中或者p标签中的内容) | Name | 标签的名字, … 的名字是‘p' 格式为:.name | Attributes | 标签的属性,字典形式组织,格式:.attrs | NavigableString | 标签内非属性字符串,<>…</>中的字符串,格式:.string | Comment | 标签内的字符串的注释部分,一种特殊的Comment类型 |
4.1 Tag标签是html中的最基本的信息组织单元,使用方式如下 from bs4 import BeautifulSouphtml = 'https://www.baidu.com'bs = BeautifulSoup(html,"html.parser")print(bs.title) # 获取title标签的所有内容print(bs.head) # 获取head标签的所有内容print(bs.a) # 获取第一个a标签的所有内容print(type(bs.a)) # 类型 在Tag标签中最重要的就是html页面中的name哈attrs属性,使用方式如下 print(bs.name)print(bs.head.name) # head 之外对于其他内部标签,输出的值便为标签本身的名称print(bs.a.attrs) # 把 a 标签的所有属性打印输出了出来,得到的类型是一个字典。print(bs.a['class']) # 等价 bs.a.get('class') 也可以使用get方法,传入属性的名称,二者是等价的bs.a['class'] = "newClass" # 对这些属性和内容进行修改print(bs.a)del bs.a['class'] # 对这个属性进行删除print(bs.a)
4.2 NavigableStringNavigableString中的string方法用于获取标签内部的文字 from bs4 import BeautifulSouphtml = 'https://www.baidu.com'bs = BeautifulSoup(html,"html.parser")print(bs.title.string)print(type(bs.title.string))
4.3 CommentComment 对象是一个特殊类型的 NavigableString 对象,其输出的内容不包括注释符号,用于输出注释中的内容 from bs4 import BeautifulSouphtml = 'https://www.baidu.com'bs = BeautifulSoup(html,"html.parser")print(bs.a)# 标签中的内容<a href="http://news.baidu.com" rel="external nofollow" rel="external nofollow" name="tj_trnews"><!--新闻--></a>print(bs.a.string) # 新闻print(type(bs.a.string)) # <class 'bs4.element.Comment'>
5. 基于bs4库的HTML内容的遍历方法在HTML中有如下特定的基本格式,也是构成HTML页面的基本组成成分 
而在这种基本的格式下有三种基本的遍历流程 三种种遍历方式分别是从当前节点出发。对之上或者之下或者平行的格式以及关系进行遍历
5.1 下行遍历下行遍历有三种遍历的属性,分别是 - contents
- children
- descendants
属性 | 说明 | .contents | 子节点的列表,将所有儿子节点存入列表 | .children | 子节点的迭代类型,用于循环遍历儿子节点 | .descendants | 子孙节点的迭代类型,包含所有子孙节点,用于循环遍历 | 使用举例 soup = BeautifulSoup(demo,'html.parser') # 循环遍历儿子节点for child in soup.body.children: print(child)# 循环遍历子孙节点 for child in soup.body.descendants: print(child) # 输出子节点的列表形式print(soup.head.contents)print(soup.head.contents[1]) # 用列表索引来获取它的某一个元素
5.2 上行遍历上行遍历有两种方式 | 到此这篇关于python爬虫beautifulsoup库使用操作教程全解(python爬虫基础入门)的文章就介绍到这了,更多相关python爬虫beautifulsoup库内容请搜索51zixue.net以前的文章或继续浏览下面的相关文章希望大家以后多多支持51zixue.net!