xpath测试1: main.py
"""=== coding: UTF8 ==="""
from lxml import etree
xml = """
<book>
<id>1</id>
<name>春风</name>
<price>1.56</price>
<nick>万里</nick>
<author>
<nick id="10086">周大庆</nick>
<nick id="10010">黄天山</nick>
<nick class="joy">周谈浩</nick>
<div>
<nick>嘟嘟</nick>
</div>
<span>
<nick>珊瑚</nick>
</span>
</author>
</book>
"""
"""
========================================
主函数功能测试
========================================
"""
if __name__ == '__main__':
tree = etree.XML(xml)
result = tree.xpath("/book/author/*/nick/text()")
print(result)
xpath测试2: test.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>测试</title>
</head>
<body>
<ul>
<li><a href="http://www.baidu.com">百度</a></li>
<li><a href="http://www.google.com">谷歌</a></li>
<li><a href="http://www.sogou.com">搜狗</a></li>
</ul>
<ol>
<li><a href="feiji">飞机</a></li>
<li><a href="dapao">大炮</a></li>
<li><a href="huoche">火车</a></li>
</ol>
<div class="job">高凡尔</div>
<div class="common">刘珂</div>
</body>
</html>
main.py
"""=== coding: UTF8 ==="""
from lxml import etree
"""
========================================
主函数功能测试
========================================
"""
if __name__ == '__main__':
parser = etree.HTMLParser(encoding='utf-8')
tree = etree.parse("test.html", parser=parser)
ol_li_list = tree.xpath("/html/body/ol/li")
for li in ol_li_list:
result = li.xpath("./a/text()")
print(result)
result = li.xpath("./a/@href")
print(result)
print(tree.xpath("/html/body/ul/li/a/@href"))
print(tree.xpath("/html/body/div[1]/text()"))
print(tree.xpath("/html/body/ol/li/a/text()"))
关注公众号,获取更多资料
|