Python之Xpath

大拿老师的Python笔记

Xpath

  • 在XML文件中查找信息的一套规则/语言,根据XML的元素或者属性,根据XML的元素或者属性进行遍历。

看一个XML文件
在这里插入图片描述
参考资料

  • http://www.w3school.com.cn/xpath/index.asp

XPath开发工具

  • 开源的XPath表达式编辑工具 : XMLQUire
  • Chrome插件 : XPath Helper
  • Firefox插件 : XPath Checker

选取节点

  • nodename : 选取此节点的所有子节点
  • / : 从根节点开始选取
    • /Student : 从根节点找一个叫Student的节点,看上述图片,只有School节点。
  • // : 选取节点,不考虑位置
    • //age : 选取出三个节点,一般组成列表返回
  • . : 选取当前节点
  • (两个点) : 选取当前节点的父亲节点
  • @:选取属性
  • xpath中查找一般按路径方法查找(/表示儿子,//表示后代)
    • School/Teacher : 返回Teacher节点
    • School/Student:返回两个Student节点
    • //Student:选取所有Student节点,不考虑位置
    • School//Age:选取School中所有Age节点
    • //@other : 选取other属性
    • //Age[@Detail] : 选取带有属性Detail的Age元素

谓语-Predicates

  • /School/Student[1] : 选取School下面的第一个Student节点
  • /School/Student[last()] : 选取School下面的最后一个Student节点
  • /School/Student[last()-1] : 选取School下面的倒数第二个Student节点
  • /School/Student[position()< 3] : 选取School下面前两个Student节点
  • //Student[@score] : 选取带有属性score的Student节点
  • //Student[@score=“99”] : 选取带有属性score并且属性值是99的Student节点
  • //Student[@score]/Age : 选取带有属性score的Student节点的子节点Age

Xpath一些操作

  • | : 或者
    • //Student[@score] | //Teacher :选取带有属性score的Student节点和Teacher节点
  • 其余不常见的Xpath运算符号包括 +,-,*,div,>,<

完结!!!

猜你喜欢

转载自blog.csdn.net/qq_45911278/article/details/112727946