The Dormouse's story
Once upon a time there were three little sisters; and their names were Elsie, Lacie and Tillie; and they lived at the bottom of a well.
...
""" ``` 这个文件不是标准,我们先将文件进行标准化。标准化我们要将html文件解析,解析我们有两个常用的解析库。 ## 解析库 | 解析器 | 使用方法 | 优势 | 劣势 | | ---------------- | -------------------------------------- | -------------------------------------------- | ----------------------------------------------- | | Python标准库 | `BeautifulSoup(markup, "html.parser")` | Python的内置标准库执行速度适中文档容错能力强 | Python 2.7.3 or 3.2.2)前 的版本中文档容错能力差 | | lxml HTML 解析器 | `BeautifulSoup(markup, "lxml")` | 速度快文档容错能力强 | 需要安装C语言库 | 第一个是系统自带的,第二个库是三方库我们需要安装。用下面命令即可安装。 ``` pip install lxml ``` 我们这里先用标准库进行解析。 ```python from bs4 import BeautifulSoup soup = BeautifulSoup(html_doc, 'html.parser') soup.prettify() print(soup) #得到下面结构化的html ""“The Dormouse's story
Once upon a time there were three little sisters; and their names were Elsie , Lacie and Tillie ; and they lived at the bottom of a well.
...
""" ``` Beautiful Soup将复杂HTML文档转换成一个复杂的树形结构,每个节点都是Python对象。 ## Tag ```python from bs4 import BeautifulSoup soup = BeautifulSoup(html_doc, 'html.parser') soup.prettify() tag = soup.b print(tag) print(type(tag)) # The Dormouse's story #The Dormouse's story``` 由于bs4内容过多这里只讲一部分知识。欢迎小伙伴收藏防止走丢。 码字不易,欢迎大家在评论区留言,收藏。或者加入[群聊](https://jq.qq.com/?_wv=1027&k=vH00muGu)一起进步学习。