how to get the source of html in lxml?

Chris Rebert clp2 at
Mon Dec 31 07:48:12 CET 2012

On Sun, Dec 30, 2012 at 10:32 PM, contro opinion <contropinion at> wrote:
> import urllib
> import lxml.html
> down=''
> file=urllib.urlopen(down).read()
> root=lxml.html.document_fromstring(file)
> body=root.xpath('//div[@class="articalContent  "]')[0]
> print body.text_content()
> When i run the code, what i get is the text content ,how can i get the html
> source code of it?

print lxml.html.tostring(body)

Did you read through the lxml.html documentation?
It includes several examples that make use of lxml.html.tostring().

RTFineM-ly Yours,

More information about the Python-list mailing list