I have to parse some HTML. However, it is malformed.. as you can see the text “Cowabunga” is not in contained in any HTML element.
from lxml.html import fromstring
from lxml.cssselect import CSSSelector
stuff = '''<p>
<span id="alpha" style="color: #999; "></span>
<span id="bravo" style="color: #999; "></span>
Cowabunga
</p>'''
l = CSSSelector ("p")
e = l(fromstring(stuff))
print e[0].text
How can I use lxml/Python to write a CSSSelector to locate this text?
Thanks
Edit: The code above gives blank output – just a row of spaces – I need to catch “Cowabunga”
This is mixed content so it’s always a little rough.
e[0].text_content()will get all of the text in your example.