I'm running into an issue when trying to get the parent node of a tr element whilst iterating through them all.
Here's a basic table that I'm working with.
<table border=1> <tbody> <tr> <td> <p>Some text</p> </td> <td> <p>Some more text</p> </td> </tr> <tr> <td> <p> Some more text</p> </td> <td> <p> Some more text</p> </td> </tr> <tr> <td> <p> Some more text</p> </td> <td> <p> Some more text</p> </td> </tr> </tbody> </table>
And here's my Python script to get the parent node using lxml
import lxml.html
htm = lxml.html.parse('plaintable.htm')
tr = htm.xpath('//tr')
for x in tr:
tbody = tr.getparent()
if tbody.index(tr) == 1:
print ('Success!')
print ('Finished')
I'm getting this error when I run the script: AttributeError: 'list' object has no attribute 'getparent'
I'm quite new to Python so it could be something simple I'm messing up. I read through the lxml documents and I couldn't find an answer.
Any help would be great!