# beautifulsoup ## 一、beautifulsoup的简单使用 简单来说,Beautiful Soup是python的一个库,最主要的功能是从网页抓取数据。官方解释如下: Beautiful Soup提供一些简单的、python式的函数用来处理导航、搜索、修改分析树等功能。 它是一个工具箱,通过解析文档为用户提供需要抓取的数据,**因为简单**,所以不需要多少代码就可以写出一个完整的应用程序。 ### 1、安装 ``` pip install beautifulsoup4 ``` #### **1.1解析器** Beautiful Soup支持Python标准库中的HTML解析器,还支持一些第三方的解析器,如果我们不安装它,则 Python 会使用 Python默认的解析器,lxml 解析器更加强大,速度更快,推荐安装。 ```python pip install lxml ``` #### 1.2解析器对比 [官网文档](http://beautifulsoup.readthedocs.io/zh_CN/latest/) ### 2、快速开始 下面的一段HTML代码将作为例子被多次用到.这是 *爱丽丝梦游仙境的* 的一段内容(以后内容中简称为 *爱丽丝* 的文档): ```python html_doc = """ <html><head><title>The Dormouse's story</title></head> <body> <p class="title"><b>The Dormouse's story</b></p> <p class="story">Once upon a time there were three little sisters; and their names were <a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>, <a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and <a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>; and they lived at the bottom of a well.</p> <p class="story">...</p> """ ``` 使用BeautifulSoup解析这段代码,能够得到一个 `BeautifulSoup` 的对象,并能按照标准的缩进格式的结构输出: ```python from bs4 import BeautifulSoup soup = BeautifulSoup(html_doc, 'lxml') # html进行美化 print(soup.prettify()) ``` 匹配代码 ```python <html> <head> <title> The Dormouse's story </title> </head> <body> <p class="title"> <b> The Dormouse's story </b> </p> <p class="story"> Once upon a time there were three little sisters; and their names were <a class="sister" href="http://example.com/elsie" id="link1"> Elsie </a> , <a class="sister" href="http://example.com/lacie" id="link2"> Lacie </a> and <a class="sister" href="http://example.com/tillie" id="link3"> Tillie </a> ; and they lived at the bottom of a well. </p> <p class="story"> ... </p> </body> </html> ``` 几个简单的浏览结构化数据的方法: ```python soup.title # 获取标签title # <title>The Dormouse's story</title> soup.title.name # 获取标签名称 # 'title' soup.title.string # 获取标签title内的内容 # 'The Dormouse's story' soup.title.parent # 获取父级标签 soup.title.parent.name # 获取父级标签名称 # 'head' soup.p # <p class="title"><b>The Dormouse's story</b></p> soup.p['class'] # 获取p的class属性值 # 'title' soup.a # <a class="sister" href="http://example.com/elsie" id="link1">Elsie</a> soup.find_all('a') # [<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>, # <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>, # <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>] soup.find(id="link3") # 获取id为link3的标签 # <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a> ``` 从文档中找到所有\<a>标签的链接: ```python for link in soup.find_all('a'): print(link.get('href')) # http://example.com/elsie # http://example.com/lacie # http://example.com/tillie ``` 从文档中获取所有文字内容: ```python print(soup.get_text()) ``` ### 3、如何使用 将一段文档传入BeautifulSoup 的构造方法,就能得到一个文档的对象, 可以传入一段字符串或一个文件句柄. ```python from bs4 import BeautifulSoup soup = BeautifulSoup(open("index.html"), 'lxml') soup = BeautifulSoup("<html>data</html>", 'lxml') ``` 然后,Beautiful Soup选择最合适的解析器来解析这段文档,如果手动指定解析器那么Beautiful Soup会选择指定的解析器来解析文档。 ### 4、对象的种类 Beautiful Soup将复杂HTML文档转换成一个复杂的树形结构,每个节点都是Python对象,所有对象可以归纳为种 `Tag` , `NavigableString` , `BeautifulSoup` , `Comment` . #### 4.1 Tag `通俗点讲就是 HTML 中的一个个标签,Tag` 对象与XML或HTML原生文档中的tag相同: ```python soup = BeautifulSoup('<b class="boldest">Extremely bold</b>') tag = soup.b type(tag) # <class 'bs4.element.Tag'> ``` ##### 4.1.2 tag的名字 soup对象再以爱丽丝梦游仙境的html_doc为例,
BS4知识点记录
于 2023-12-27 21:22:06 首次发布