Python如何实现XPath查询?

2026-05-21 18:294阅读0评论SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计366个文字,预计阅读时间需要2分钟。

Python如何实现XPath查询?

XPath是一种用于在XML文件中查找信息的规则和语言。它通过根元素或属性来遍历XML元素。例如,使用XPath可以查找特定元素或属性。以下是一些XPath工具和编辑器:

- XMLQuire- Chrome插件:XPath Helper


XPath

  • 在XML文件中查找信息的一套规则/语言,根据XML元素或者属性进行遍历
  • ​​www.w3school.com.cn/xpath/index.asp​​

XPath 开发工具

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

选取节点

  • nodename: 选取此节点的所有子节点
  • /: 从根节点开始选取
/Student: 没有记过
/School: 选取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, >, <


Python如何实现XPath查询?

本文共计366个文字,预计阅读时间需要2分钟。

Python如何实现XPath查询?

XPath是一种用于在XML文件中查找信息的规则和语言。它通过根元素或属性来遍历XML元素。例如,使用XPath可以查找特定元素或属性。以下是一些XPath工具和编辑器:

- XMLQuire- Chrome插件:XPath Helper


XPath

  • 在XML文件中查找信息的一套规则/语言,根据XML元素或者属性进行遍历
  • ​​www.w3school.com.cn/xpath/index.asp​​

XPath 开发工具

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

选取节点

  • nodename: 选取此节点的所有子节点
  • /: 从根节点开始选取
/Student: 没有记过
/School: 选取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, >, <


Python如何实现XPath查询?