XPath提供了很多节点轴选择方法,包括获取祖先节点、兄弟节点、子孙节点等。本节将介绍XPath中一些常用的节点轴。
下面的例子使用XPath和索引得到特定的<a>节点,并输出<a>节点的文本。
from lxml import etree
parser = etree.HTMLParser()
text = '''
<html>
<head>
<meta charset="UTF-8">
<title>XPath演示</title>
</head>
<body class="item">
<div>
<ul class="item" >
<li class="item1"><a href="https://geekori.com"> geekori.com</a></li>
<li class="item2"><a href="https://www.jd.com">京东商城</a>
<value url="https://geekori.com"/>
<value url="