python库的解析--xpath语法

xpath语法

表达式 描述
nodename 选取此节点的所有子节点。
/ 从根节点选取。
// 从匹配选择的当前节点选择文档中的节点,而不考虑它们的位置。
. 选取当前节点。
选取当前节点的父节点。
@ 选取属性。
text() 获取文本内容
[nodename] 根据节点筛选
contains(@属性,“内容”) 模糊查询
import lxml.etree as le

text = '''
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title></title>
    </head>
    <body>
        <ul>
            <li class"item-0"><a href="link1.html">first item</a></li>
            <li class"item-1"><a href="link2.html">second item</a></li>
            <li class"item-inactive"><a href="link3.html">third item</a></li>
            <li class"item-1"><a href="link4.html">fourth item</a></li>
            <li class"item-0"><a href="link5.html">fifth item</a>
        </ul>
    </body>
</html>
'''


# with open('./test.html', 'w') as f:
#     f.write(text)
# 创建XPath对象
html = le.HTML(text)
'''
    直接读取文本文件并进行解析
    html = le.parse('./test.html', le.HTMLParser())
'''

# 修正html文件并转换为字节流(bytes)
result = le.tostring(html)

result = le.HTML(result.decode('utf-8'))

a_xs = result.xpath("//ul/li/a/@href")

for a_x in a_xs:
    print(a_x)

xpath轴

轴名称 结果
ancestor 选取当前节点的所有先辈(父、祖父等)。
ancestor-or-self 选取当前节点的所有先辈(父、祖父等)以及当前节点本身。
attribute 选取当前节点的所有属性。
child 选取当前节点的所有子元素。
descendant 选取当前节点的所有后代元素(子、孙等)。
descendant-or-self 选取当前节点的所有后代元素(子、孙等)以及当前节点本身。
following 选取文档中当前节点的结束标签之后的所有节点。
following-sibling 选取当前节点结束标签之后的所有同级节点。
namespace 选取当前节点的所有命名空间节点。
parent 选取当前节点的父节点。
preceding 选取文档中当前节点的开始标签之前的所有节点。
preceding-sibling 选取当前节点之前的所有同级节点。
self 选取当前节点。
import lxml.etree as le

text = '''
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title></title>
    </head>
    <body>
        <ul>
            <li class"item-0"><a href="link1.html">first item</a></li>
            <li class"item-1"><a href="link2.html">second item</a></li>
            <li class"item-inactive"><a href="link3.html">third item</a></li>
            <li class"item-1"><a href="link4.html">fourth item</a></li>
            <li class"item-0"><a href="link5.html">fifth item</a>
        </ul>
    </body>
</html>
'''


# with open('./test.html', 'w') as f:
#     f.write(text)
# 创建XPath对象
html = le.HTML(text)
'''
    直接读取文本文件并进行解析
    html = le.parse('./test.html', le.HTMLParser())
'''

# 修正html文件并转换为字节流(bytes)
result = le.tostring(html)

result = le.HTML(result.decode('utf-8'))

# 使用轴的时候必须在轴名称后加上::
print(result.xpath("//li[1]/following-sibling::*"))

xpath运算符

运算符 描述 实例 返回值
| 计算两个节点集 //book | //cd 返回所有拥有 book 和 cd 元素的节点集
+ 加法 6 + 4 10
- 减法 6 - 4 2
* 乘法 6 * 4 24
div 除法 8 div 4 2
mod 计算除法的余数 5 mod 2 1
= 等于 price=9.80 如果 price 是 9.80,则返回 true。如果 price 是 9.90,则返回 false。
!= 不等于 price!=9.80 如果 price 是 9.90,则返回 true。如果 price 是 9.80,则返回 false。
< 小于 price<9.80 如果 price 是 9.00,则返回 true。如果 price 是 9.90,则返回 false。
<= 小于或等于 price<=9.80 如果 price 是 9.00,则返回 true。如果 price 是 9.90,则返回 false。
> 大于 price>9.80 如果 price 是 9.90,则返回 true。如果 price 是 9.80,则返回 false。
>= 大于或等于 price>=9.80 如果 price 是 9.90,则返回 true.如果 price 是 9.70,则返回 false。
or price=9.80 or price=9.70 如果 price 是 9.80,则返回 true。如果 price 是 9.50,则返回 false。
and price>9.00 and price<9.90 如果 price 是 9.80,则返回 true。如果 price 是 8.50,则返回 false。
import lxml.etree as le

text = '''
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title></title>
    </head>
    <body>
        <ul>
            <li class"item-0"><a href="link1.html">first item</a></li>
            <li class"item-1"><a href="link2.html">second item</a></li>
            <li class"item-inactive"><a href="link3.html">third item</a></li>
            <li class"item-1"><a href="link4.html">fourth item</a></li>
            <li class"item-0 new" id="here"><a href="link5.html">fifth item</a>
        </ul>
    </body>
</html>
'''


# with open('./test.html', 'w') as f:
#     f.write(text)
# 创建XPath对象
html = le.HTML(text)
'''
    直接读取文本文件并进行解析
    html = le.parse('./test.html', le.HTMLParser())
'''

# 修正html文件并转换为字节流(bytes)
result = le.tostring(html)

result = le.HTML(result.decode('utf-8'))

# 属性多值匹配(用模糊匹配)
print(result.xpath("//li[contains(@class, 'new')]/a/text()"))

# 多属性匹配(用运算符)
print(result.xpath("//li[contains(@class, 'new') and @id='here']/a/text()"))

猜你喜欢

转载自blog.csdn.net/hide_in_darkness/article/details/108277566