Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using xpath to select an element after another

Tags:

xpath

lxml

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']")
like image 809
jseabold Avatar asked Oct 09 '13 18:10

jseabold


2 Answers

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

like image 178
paul trmbrth Avatar answered Oct 14 '22 18:10

paul trmbrth


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

 //table[preceding::p[1]/b[.='Header 2']]
like image 22
Arup Rakshit Avatar answered Oct 14 '22 19:10

Arup Rakshit