Reputation: 6753
I just downloaded lxml to parse broken HTML documents. I was reading through the documentation of lxml but could not find that given a HTML document, how do we just retrieve the text in the document using lxml. I will be obliged if someone could help me with this.
Upvotes: 0
Views: 298
Reputation: 22868
It's very simple:
from lxml import html
html_document = ... #Get your document contents here from a file or whatever
tree = html.fromstring(html_document)
text_document = tree.text_content()
If you only want the content from specific blocks (e.g. the body block), then you can access them using xpath expressions:
body_tags = tree.xpath('//body')
if body_tags:
body = body_tags[0]
text_document = body.text_content()
else:
text_document = ''
Upvotes: 1