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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值