Python网络爬虫——HTML解析之BeautifulSoup

巧了我就是萌 提交于 2019-11-27 15:46:14

BeautifulSoup是一个用于从HTML和XML文件中提取数据的Python库。BeautifulSoup提供一些简单的函数来处理导航、搜索、修改分析树等功能。BeautifulSoup模块中的查找提取功能非常强大,而且非常便捷,可以节省程序员数小时或数天的时间。
BeautifulSoup自动将输入文档转换为Unicode编码,输出文档转换为utf-8编码,用户不需要考虑编码方式,除非文档没指定一个编码方式,那么BeautifulSoup就不能自动识别编码方式。

  1. BeautifulSoup的安装
    在https://www.crummy.com/software/BeautifulSoup/bs4/download/下载BeatifulSoup的源码,进cmd中,进入BeatifulSoup4-4.8.0的存储路径,在输入Python steup.py install命令即可。
    在这里插入图片描述
    在这里插入图片描述
  2. BeatifulSoup的使用
    将BeatifulSoup安装完成后,我们就可以来使用它了。
    (1)导入bs4库,然后创建一个模拟HTML代码的字符串,代码如下:
from bs4 import BeautifulSoup      #导入BeatifulSoup 库


#创建模拟HTML代码的字符串

html_doc = """
<html><head><title>The Dotmouse's story</title></head>
<body>
<p class="title"><b>The Dotmouse's story</b></p>
<p class="story">Once upon a time there were three little sisiter ;and therinames were
<a href ="http ://example.com/elsie" class ="sister" id ="link1">Elsie</a>,
<a href ="http ://example.com/lacie" class ="sister" id ="link2">Lasie</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>

"""
#创建一个BeatifulSoup对象,获取页面正文
soup = BeautifulSoup(html_doc,features="lxml")
print(soup)         #打印解析的HTML代码

注意:
如出现bs4.FeatureNotFound: Couldn’t find a tree builder with the features you requested: lxml. Do you need to install a parser library?就在Python的安装目录下的\Scripts中去安装lxml这个解析器。
安装方法可参照https://www.jb51.net/article/142670.htm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!