lxml.etree, element.text doesn't return the entire text from an element

user522034 picture user522034 · Jan 22, 2011 · Viewed 11.9k times · Source

I scrapped some html via xpath, that I then converted into an etree. Something similar to this:

<td> text1 <a> link </a> text2 </td>

but when I call element.text, I only get text1 (It must be there, when I check my query in FireBug, the text of the elements is highlighted, both the text before and after the embedded anchor elements...

Answer

Teddy picture Teddy · Jan 23, 2011

Use element.xpath("string()") or lxml.etree.tostring(element, method="text") - see the documentation.