使用XPath获取第二个元素文本? [英] Get second element text with XPath?

查看:124
本文介绍了使用XPath获取第二个元素文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<span class='python'>
  <a>google</a>
  <a>chrome</a>
</span>

我想获取chrome并使其已经像这样工作.

I want to get chrome and have it working like this already.

q = item.findall('.//span[@class="python"]//a')
t = q[1].text # first element = 0

我想将其组合到单个XPath表达式中,而只获得一项而不是列表.
我尝试了此操作,但它不起作用.

I'd like to combine it into a single XPath expression and just get one item instead of a list.
I tried this but it doesn't work.

t = item.findtext('.//span[@class="python"]//a[2]') # first element = 1

实际的而不是简化的HTML就是这样.

And the actual, not simplified, HTML is like this.

<span class='python'>
  <span>
    <span>
      <img></img>
      <a>google</a>
    </span>
    <a>chrome</a>
  </span>
</span>

推荐答案

我尝试了这个,但是没有用.

I tried this but it doesn't work.

t = item.findtext('.//span[@class="python"]//a[2]')

这是有关//缩写的常见问题解答.

This is a FAQ about the // abbreviation.

.//a[2]的意思是:选择当前节点的所有后代,它们是其父级的第二个a子代.因此,根据具体的XML文档,它可以选择多个元素或不选择任何元素.

.//a[2] means: Select all a descendents of the current node that are the second a child of their parent. So this may select more than one element or no element -- depending on the concrete XML document.

为了简单起见,[]运算符的优先级高于//.

To put it more simply, the [] operator has higher precedence than //.

如果您只想返回所有节点中的一个(第二个),则必须使用方括号来强制您想要的优先级:

If you want just one (the second) of all nodes returned you have to use brackets to force your wanted precedence:

(.//a)[2]

这实际上选择了当前节点的第二个a后代.

This really selects the second a descendent of the current node.

对于问题中使用的实际表达式,将其更改为:

(.//span[@class="python"]//a)[2]

或将其更改为:

(.//span[@class="python"]//a)[2]/text()

这篇关于使用XPath获取第二个元素文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆