当前位置:网站首页>11 Beautiful Soup 解析库的简介及安装
11 Beautiful Soup 解析库的简介及安装
2022-06-21 17:33:00 【安迪python学习笔记】
11 Beautiful Soup 解析库的简介及安装
文章目录
我们要完成一个爬虫任务需要以下3个步骤:
步骤1:获取网页;获取网页即发起网页请求,获取网页源代码。
步骤2:解析网页;解析网成首先要完成解析数据,然后再提取数据。
步骤3:存储数据。
Python 要处理csv文件,需要先生成一个 Python 对象,即 csv 对象。
Python 要处理获取到的 HTML 网页,也需要先生成一个 Python 对象。
解析数据的作用就是要将 HTML文档转化为 Python 程序可处理的 Python 对象。
解析数据需要使用解析库和解析器才能实现。
Beautiful Soup 和Xpath 都是用于解析数据的解析库。
11.1 Beautiful Soup 解析库简介
11.1.1 Beautiful Soup 的特点

1.API简单、功能强大
Beautiful Soup 提供一些简单的方法和Python式函数,用于浏览、搜索和和修改解析树,它是一个工具箱,通过解析文档为用户提供需要抓取的数据。
Beautiful Soup 和Xpath 都可以从文档中提取数据。
但Beautiful Soup 还可以修改文档中的数据,这是Xpath 不具备的功能。
2. 支持多种解析器
Beautiful Soup 在使用时需要给它安装解析器。
Beautiful Soup 支持多种解析器。
包括Python标准库中的HTML解析器,也支持第三方解析器。
3. 自动实现编码的转换
Beautiful Soup 自动将输入文档转换成Unicode类型,输出文档转换为uft-8编码。
如果文档中没指定编码,只需我们指定编码即可。
11.2 Beautiful Soup 解析库的安装
目前Beautiful Soup 的最新版本是4.x,之前的版本已经停止开发了。
所以今后的解析都用beautifulsoup4 。
beautifulsoup4 简写为bs4。
bs4 库是Python的第三方库,使用前必须先安装后导入。
bs4 的 4 表示版本。
安装命令
pip3 install beautifulsoup4
11.3 Beautiful Soup 解析器

解析器的安装方法
安装lxml
Beautiful Soup 的LXML HTML解析器是依赖于lxml库的,使用之前需要先安装lxml库。
安装命令
pips install lxml
安装html5lib
安装命令
pips install html5lib
边栏推荐
- 空中操作仅通过距离映射对遮挡目标进行鲁棒定位(RAL2022)
- Collaborative filtering
- C语言刷题随记 —— 求 s=a+aa+aaa+aaaa+aa...a 的值
- C2—Qt实现串口调试助手2021.10.21
- # bash 的 try catch
- College Physics Chapter 9 vibration
- 老师们,oracle-cdc遇到不能解析的dml语句,因为这个语句里面有个字段是比较特殊的空间地理位
- Make interface automation test flexible
- 從“村辦企業”到“百億集團”,紅星實業何以完成“蝶變”?
- 36 krypton launched | focusing on the innovation of health insurance products, and "Yingshi health" has obtained four rounds of financing
猜你喜欢
随机推荐
Image classification, AI and automatic performance test
JDBC Basics
Explanation of El table paging select all function
ACL 2022 | 基于自监督图对齐的多语言知识图谱推理
College Physics Chapter 9 vibration
Regional competitions in recent years (20-22)
Crawling frog SEO spider
动态加载资源之AssetDatabase
协同过滤(Collaborative Filtering)
秒云云原生信创全兼容解决方案再升级,助力信创产业加速落地
Internet communication process
Disclose the design idea of MVVM framework supporting Baidu search, feed and applet, and San core personnel have made great efforts to build it
A test tool for recording and playback interface based on mitmproxy
Foreign capital staged a "successful flight" and domestic capital took over the offer. Is New Oriental online "square"?
Rk3566 commissioning gc2053
De « l'entreprise gérée par le village » au « Groupe de 10 milliards de yuans », comment l'industrie Hongxing complète - t - elle le « changement de papillon »?
JSP 基本知识
How to apply for SSL certificate using IP
JSON parsing of node
Servlet specification (I)
![[HCTF 2018]WarmUp](/img/b0/6baee8ac76b56378230c2218f15734.png)








