using xpath to select an element after another

寵の児 提交于 2019-12-21 07:38:57

问题


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']")

回答1:


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

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



回答2:


Some alternatives to @Arup's answer:

tree.xpath("//p[b='Header 2']/following-sibling::table[1]")

select the first table sibling following the p containing the b header containing "Header 2"

tree.xpath("//b[.='Header 2']/following::table[1]")

select the first table in document order after the b containing "Header 2"

See XPath 1.0 specifications for details on the different axes:

  • the following axis contains all nodes in the same document as the context node that are after the context node in document order, excluding any descendants and excluding attribute nodes and namespace nodes

  • the following-sibling axis contains all the following siblings of the context node; if the context node is an attribute node or namespace node, the following-sibling axis is empty



来源:https://stackoverflow.com/questions/19279842/using-xpath-to-select-an-element-after-another

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!