使用lxml的HTML和parse两种方式解析html代码

使用lxml解析html代码:

若解析的代码是字符串类型

使用lxml.etree.HTML进行解析,例如

from lxml import etree

text="""
<div class="login_corp" >
<div class="Third-partyi-login">
	<a title="微信" class="login-item weixin" href="http://www.renren.com/api/jump?src=wx" id="login_weixin" stats="loginPage_weixin_link"></a>
	 <a title="QQ" class="login-item qq" href="http://www.renren.com/api/jump?src=qq" id="login_qq" stats="loginPage_qq_link"></a>
	 <a title="微博" class="login-item weibo" href="http://www.renren.com/api/jump?src=wb" id="login_weibo" stats="loginPage_weibo_link"></a>
</div>
</div>
<div class="other-login clearfix">
<div class="login-word login-item">其它账号登录:</div>
<a  title="移动" class="login-item yidong" href="https://open.mmarket.com:443/omee-aus/services/oauth/authorize?responseType=code&scope=getUserInfo&clientId=300007884008&redirectUri=http%3A%2F%2Fwww.renren.com%2Fbind%2Fcnmobile%2FloginCallBack&clientState=9" id="login_cnmobile" stats="loginPage_baidu_link"></a>
<a title="天翼" class="login-item tianyi" id="login_tianyi" href="https://oauth.api.189.cn/emp/oauth2/authorize?app_id=296961050000000294&response_type=code&redirect_uri=http://www.renren.com/bind/ty/tyLoginCallBack" stats="loginPage_tianyi_link"></a>
<a title="360" class="login-item lo360" id="login_360" href="https://openapi.360.cn/oauth2/authorize?client_id=5ddda4458747126a583c5d58716bab4c&response_type=code&redirect_uri=http://www.renren.com/bind/tsz/tszLoginCallBack&scope=basic&display=default" stats="loginPage_360_link"></a>
 <a title="百度" class="login-item baidu" href="https://openapi.baidu.com/oauth/2.0/authorize?response_type=code&client_id=foRRWjPq8In3SIhmKQw1Pep3&redirect_uri=http%3A%2F%2Fwww.renren.com%2Fbind%2Fbaidu%2FbaiduLoginCallBack" id="login_baidu" stats="loginPage_baidu_link"></a>
</div>
"""#这已经是规范后的代码
html = etree.HTML(text)
print(etree.tostring(html,encoding='utf-8').decode('utf-8'))

在这里插入图片描述
看图片的第一行,会发现它多出了一些东西。☺☺

解析html文件时

使用lxml.etree.parse进行解析

from lxml import etree
html = etree.parse('renren.html')
print(etree.tostring(html,encoding='utf-8').decode('utf-8'))

若出现报错
报错形式
就改成以下代码:

from lxml import etree
parser = etree.HTMLParser(encoding='utf-8')#自己创建的html解析器
html = etree.parse('renren.html',parser=parser)
print(etree.tostring(html,encoding='utf-8').decode('utf-8'))

这是因为parse默认用的是xml解析器,如果碰到一些不规范的html代码时就会解析错误,这时就要自己创建html解析器。

BeautifulSoup4解析代码

相比于lxml,他的性能较低,主要不能够直接针对性的对某个标签(例如a标签)进行解析,它直接就将该hxml文档下载解析成一个树结构。所以花的时间和内存就较大,导致性能降低。
但是,它的用法比较简单,支持css选择器、正则解析器、xlmx解析器和python的html解析器。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值