# BeautifulSoup(一) 通过 urlopen 获取了网站的一个网页 HTML 文本后,我们就拥有了数据;但是,不能被处理为信息的数据是没有用的,所以我们就需要 BeautifulSoup 对象对数据进行解析。 通过 BeautifulSoup 库的 BeautifulSoup 对象以一定方式(即通过解析器)解析 html 文本对象并返回有一个对象,该对象可以通过 `.` 来获取属性(html标签)中的文本或标签属性等。 当创建一个 BeautifulSoup 对象时,需要传入两个参数: 1. 该对象所基于的 HTML 文本 2. 解析器 常见的解析器有三种(解析器对处理 html 文本的速度自上而下提高): - `html.parser` - `lxml` - `html5lib` 第一个解析器是 python3 的一个解析器,不需要额外安装依赖库;后两个解析器的依赖库是需要额外安装的。【`pip3 install lxml`;\`\`pip3 install html5lib\`\`】 ```python # way1 bs = BeautifulSoup(html.read(),'html.parser') # way2 bs = BeautifulSoup(html.read(),'lxml') # way3 bs = BeautifulSoup(html.read(),'html5lib') ``` 一般而言,使用第一种解析器即 html.parser 即可,因为使用它不会出现可移植性和易用性的问题。只有当需要处理复杂的 HTML 文本且对文本处理速度要求较高时才使用后两种解析器。 ```python from urllib.request import urlopen from bs4 import BeautifulSoup html = urlopen('https://www.hzu.edu.cn/main.htm') content = html.read() bs = BeautifulSoup(content, 'html.parser') print(bs.title) ``` ```guess # code run result