Something ...">

How to select parent based on child in lxml?

I have this code:

<table cellspacing="1" cellpadding="1" border="0">
  <tbody>
   <tr>
    <td>Something else</td>
   </tr>
   <tr>
    <td valign="top">
      <a href="http://exact url">Something</a>
    </td>
    <td valign="top">Something else</td>
   </tr>
  </tbody>
</table>

I want to find a table, but it is very difficult to set up (the same code is used as 10 times). But I know what is in the URL. How can I get the parent table?

+5
source share
4 answers

If there tis etreean XML for this snippet, then the link you are looking for is

t.xpath('//a[@href = "http://exact url"]')[0]

From there you can go to tableusing the axis ancestor:

t.xpath('//a[@href = "http://exact url"]/ancestor::table')[-1]
+4
source

Filter tables using []. Note that the attribute is the grandson//table[.//@href="blah"]

Or //a[@href="blah"]//ancestor::table

+2
source

XPath.

(//a[@href = "http://exact url"])[1]/ancestor::table[1]

table a XML, href - "http://exact url".

This provides the correct element table, even if nested tables , each of which has the desired element aas a descendant. In this case, the aforementioned XPath expression selects the innermost one table- unlike the currently accepted answer, which receives the outermost ancestor table.

+2
source

//a[@href="http://exact url"]/../../..

To reach the table element, you need 3 ...

0
source

All Articles