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?
答案 0 :(得分:2)
./ancestor::table[1]
这将获得相对于上下文节点最近(宏)父/祖先的table
元素。