Reputation: 1252
Is there a method to get a parent node with .xpath for item of type Element()?
I has HTML like this:
<table> ...
<tr><td>...</td></tr>
<tr><td><h3 class="product_name">Product</h3></td></tr>
</table>
I want to get a table element, where table is the first parent element for h3 of table 'table'.
from lxml.html import fromstring
html = fromstring
for h3 in html.xpath('//h3[@class="product_name"]'):
parent_table = h3.xpath('???')
What xpath should I use?
Upvotes: 3
Views: 1176
Reputation: 16085
./ancestor::table[1]
This will get the table
element that is the closest (grand)parent/ancestor relative to the context node.
Upvotes: 3