使用xpath依次选择一个元素 [英] using xpath to select an element after another

查看:79
本文介绍了使用xpath依次选择一个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了类似的问题,但是我看到的解决方案在以下方面不起作用.我离XPath专家还很远.我只需要解析一些HTML.我如何选择表头2后面的表格.我认为下面的解决方案应该可以,但显然不行.有人可以帮我吗?

I've seen similar questions, but the solutions I've seen won't work on the following. I'm far from an XPath expert. I just need to parse some HTML. How can I select the table that follows Header 2. I thought my solution below should work, but apparently not. Can anyone help me out here?

content = """<div>
<p><b>Header 1</b></p>
<p><b>Header 2</b><br></p>
<table>
<tr>
    <td>Something</td>
</tr>
</table>
</div>
"""

from lxml import etree
tree = etree.HTML(content)
tree.xpath("//table/following::p/b[text()='Header 2']")

推荐答案

您需要使用以下 XPATH 1.0 使用轴 preceding .

You need to use the below XPATH 1.0 using the Axes preceding.

 //table[preceding::p[1]/b[.='Header 2']]

这篇关于使用xpath依次选择一个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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