Try creating a tree using the html parser. Also note that if it text.txtis a file, it will need to be read first.
with open('text.txt', 'r', encoding='utf8') as f:
text_html = f.read()
like this:
from lxml import etree, html
def build_lxml_tree(_html):
tree = html.fromstring(_html)
tree = etree.ElementTree(tree)
return tree
tree = build_lxml_tree(text_html)
result = tree.xpath('//title')
print(result)
source
share