lxml基本语法

"""
Author: 余婷
Time: 2022/8/18 09:22
Good Good Study, Day Day Up!
"""
from lxml import etree

# 1. xpath基本概念
"""
1)树:整个html(xml)代码结构就是一个树结构
2)节点:树结构中的每一个元素(标签)就是一个节点
3)根节点(根元素): html或者xml最外面的那个标签(元素)
4)节点内容:标签内容
5)节点属性:标签属性
"""

# 2. xml数据格式
# xml和json一样,是一种通用的数据格式(绝大部分编程语言都支持的数据格式)
"""
xml是通过标签(元素)的标签内容和标签属性来保存数据的。

示例:保存一个超市信息
1)json数据
{
    "name": "永辉超市",
    "address": "肖家河大厦",
    "staffs":[
        {"name":"小明", "id": "s001", "position": "收营员", "salary": 4000},
        {"name":"小花", "id": "s002", "position": "促销员", "salary": 3500},
        {"name":"张三", "id": "s003", "position": "保洁", "salary": 3000},
        {"name":"李四", "id": "s004", "position": "收营员", "salary": 4000},
        {"name":"王五", "id": "s005", "position": "售货员", "salary": 3800}
    ],
    "goodsList":[
        {"name": "泡面", "price": 3.5, "count": 120, "discount":0.9},
        {"name": "火腿肠", "price": 1.5, "count": 332, "discount":1},
        {"name": "矿泉水", "price": 2, "count": 549, "discount":1},
        {"name": "面包", "price": 5.5, "count": 29, "discount":0.85}
    ]
}

xml数据:
<supermarket name="永辉超市" address="肖家河大厦">
    <staffs>
        <staff  id="s001">
            <name>小明</name>
            <position>收营员</position>
            <salary>4000</salary>
        </staff>
        <staff  id="s002">
            <name>小花</name>
            <position>促销员</position>
            <salary>3500</salary>
        </staff>
        <staff  id="s003">
            <name>张三</name>
            <position>保洁</position>
            <salary>3000</salary>
        </staff>
        <staff  id="s004">
            <name>李四</name>
            <position>收营员</position>
            <salary>4000</salary>
        </staff>
        <staff  id="s005">
            <name>王五</name>
            <position>售货员</position>
            <salary>3800</salary>
        </staff>
    </staffs>
    
    <goodsList>
        <goods discount="0.9">
            <name>泡面</name>
            <price>3.5</price>
            <count>120</count>
        </goods>
        <goods>
            <name>火腿肠</name>
            <price>1.5</price>
            <count>332</count>
        </goods>
        <goods>
            <name>矿泉水</name>
            <price>2</price>
            <count>549</count>
        </goods>
        <goods discount="8.5">
            <name>面包</name>
            <price>5.5</price>
            <count>29</count>
        </goods>
    </goodsList>
</supermarket>
"""

# 3. xpath语法
# 1)创建树结构获取树的根节点
# etree.XML(xml数据)
# etree.HTML(html数据)
f = open('files/data.xml', encoding='utf-8')
root = etree.XML(f.read())
f.close()

# 2)根据xpath获取指定标签
# 节点对象.xpath(路径)    -   返回路径对应的所有的标签,返回值是列表,列表中的元素是标签对象(节点对象)
"""
路径的写法:
1. 绝对路径:   用"/"开头的路径  -   /标签在树结构中的路径    (路径必须从根节点开始写)
2. 相对路径:   路径开头用"."标签当前节点(xpath前面是谁,'.'就代表谁), ".."表示当前节点的上层节点
3. 全路径:     用"//"开头的路径  -   在整个树中获取标签

注意:绝对路径和全路径的写法以及查找方式和是用谁去点的xpath无关
"""
result = root.xpath('/supermarket/staffs/staff/name/text()')
print(result)

result = root.xpath('./staffs/staff/name/text()')
print(result)

staff1 = root.xpath('./staffs/staff')[0]            # 获取第一个员工对应的staff标签
result = staff1.xpath('./name/text()')
print(result)       # ['小明']

result = staff1.xpath('../staff/name/text()')
print(result)       # ['小明', '小花', '张三', '李四', '王五']

result = root.xpath('//name/text()')
print(result)

result = staff1.xpath('//goods/name/text()')
print(result)

# 3)获取标签内容
# 节点对象.xpath(获取标签的路径/text())        -       获取指定路径下所有标签的标签内容
result = root.xpath('//position/text()')
print(result)

# 4)获取标签属性值
# 节点对象.xpath(获取标签的路径/@属性名)
result = root.xpath('/supermarket/@name')
print(result)       # ['永辉超市']

result = root.xpath('//staff/@id')
print(result)

# 5)谓语(条件)
# a. 位置相关谓语
"""
[N]     -      第N个
[last()]    -   最后一个
[last()-N];   [last()-1] -  倒数第2个 、 [last()-2] - 倒数第3个
[position()>N]、[position()<N]、[position()>=N]、[position()<=N]
"""
result = root.xpath('//staff[1]/name/text()')
print(result)       # ['小明']

result = root.xpath('//staff[last()]/name/text()')
print(result)       # ['王五']

result = root.xpath('//staff[last()-1]/name/text()')
print(result)       # ['李四']

result = root.xpath('//staff[position()<3]/name/text()')
print(result)   # ['小明', '小花']

# b.属性相关谓语
"""
[@属性名=属性值]      -      指定属性是指定值的标签
[@属性名]      -   拥有指定属性的标签
"""
# staff[@class="c1"] == staff.c1
result = root.xpath('//staff[@class="c1"]/name/text()')
print(result)

result = root.xpath('//staff[@id="s003"]/name/text()')
print(result)

result = root.xpath('//goods[@discount]/name/text()')
print(result)

# c.子标签内容相关谓语       -    根据子标签的内容来筛选标签
"""
[子标签名>数据]
[子标签名<数据]
[子标签名>=数据]
[子标签名<=数据]
[子标签名=数据]
"""
result = root.xpath('//goods[price=2]/name/text()')
print(result)

# 6)通配符  - 写路径的时候用*来表示所有标签或者所有属性
result = root.xpath('//staff[1]/*/text()')
print(result)

# *[@class="c1"]  == .c1
result = root.xpath('//*[@class="c1"]/name/text()')
print(result)

result = root.xpath('//goods[@*]/name/text()')
print(result)


result = root.xpath('/supermarket/@*')
print(result)


# 7)若干路径 - |
# 路径1|路径2       -      同时获取路径1和路径2的内容
result = root.xpath('//goods/name/text()|//staff/position/text()')
print(result)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值