lxml是python中的一个解析库,支持html和xml解析,支持XPath解析方式,而且解析效率非常高。lxml属于第三方库,使用前要安装lxml库。
安装命令:
pip install lxml
查看是否安装
(1)、命令行中执行 pip list,看输出的结果中是否有lxml
(2)、python命令中输入import lxml,如果没有报错,说明安装成功
lxml库解析html使用:
(1)、导入lxml库的etree模块,然后使用etree.HTML(htmlcode)方法进行初始化,构造一个XPath解析对象。
doc=etree.HTML(html_pagesource)
(2)、接下来就可以使用上面获取的对象下xpath()方法来获取指定元素的对象
result=doc.xpath("//title")[0] #获取title标签对象
print(result.text) #输出标签里面的内容
print(result.attrib) #输出标签的属性值,字典类型,如果有 href属性,可调取 print(result.attrib['href'])
print(etree.tounicode(result)) #可获取result对象的html代码
result=doc.xpath("//div[@class='post']") #获取所有class属性值为post的div标签
print(result.attrib)
下面示例代码供参考
from lxml import etree
#要分析的页面源代码,也可以通过网络抓取或读取文件
html_pagesource='''
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>php技术分享博客|个人php技术博客</title>
</head>
<body class="multi index">
<div id="divAll">
<div id="divNavBar">
<ul>
<li id="nvabar-item-index"><a href="/">首页</a></li>
<li id="navbar-category-2"><a href="/list/2.html">php基础知识</a></li>
<li id="navbar-category-3"><a href="/list/3.html">php安全知识</a></li>
<li id="navbar-category-4"><a href="/list/4.html">php疑难问题</a></li>
<li id="navbar-category-7"><a href="/list/7.html">php进阶开发</a></li>
<li id="navbar-category-6"><a href="/list/6.html">python学习</a></li>
<li id="navbar-page-2"><a href="/aboutus">关于我</a></li>
</ul>
</div>
<div id="divMain">
<div class="post multi">
<h2 class="post-title"><a href="/article/29.html">使用selenium获取网址所加载所有资源url列表信息</a><span class="post-date">2021-01-07 01:01</span></h2>
<h6 class="post-footer"> 分类:python <small>|</small> 浏览:1086 <small>|</small> 评论:2 </h6>
</div>
<div class="post multi">
<h2 class="post-title"><a href="/article/28.html">mysql中索引类型Btree和Hash的区别以及使用场景</a><span class="post-date">2020-11-16 23:50</span></h2>
<h6 class="post-footer"> 分类:mysql学习 <small>|</small> 浏览:544 <small>|</small> 评论:3 </h6>
</div>
<div class="post multi">
<h2 class="post-title"><a href="/article/27.html">selenium在Centos服务器下环境搭建</a><span class="post-date">2020-11-04 19:21</span></h2>
<h6 class="post-footer"> 分类:python <small>|</small> 浏览:729 <small>|</small> 评论:0 </h6>
</div>
</div>
</div>
</body>
</html>
'''
doc=etree.HTML(html_pagesource)
title=doc.xpath('//title')[0].text
print(title) #输出标题内容
#获取分类列表,栏目名称及栏目链接
cates=doc.xpath('//div[@id="divNavBar"]/ul/li/a')
for cate in cates:
print(cate.text,cate.attrib['href'])
#获取文章列表
infos=[]
articles=doc.xpath('//div[@class="post multi"]')
for article in articles:
info={}
title_elm=article.xpath('.//a')[0]
info['title']=title_elm.text
info['href']=title_elm.get('href')
info['dateline']=article.xpath('.//span[@class="post-date"]')[0].text
#other=article.xpath('./h6[@class="post-footer"]')[0].text
#输出不完整
other=etree.tounicode(article.xpath('.//h6[@class="post-footer"]')[0],method="text")
others=other.split('|')
for key,i in enumerate(others):
if key==0:
info['cate']=i.replace('分类:','').strip()
elif key==1:
info['views']=i.replace('浏览:','').strip()
elif key==2:
info['replies']=i.replace('评论:','').strip()
print(info)
发表评论 取消回复