I am using Python lxml library for parsing HTML.
I have got leaf point such as <a> and want to get its parent’s text.
I tried doing it in two ways:
.xpath('::parent*'),.getparent().text.
from lxml.etree import HTML
text = '<p>FIRST PART<a href="THE LINK" target="_blank">LINK TEXT</a>SECOND PART</p>'
parsed = HTML(text)
parsed.xpath('//a')[0].getparent().text
parsed.xpath('//a/parent::*')[0].text
I got FIRST PART using either approach, but how can I obtain the SECOND PART?