Get the inner HTML of a element in lxml

Sudip Kafle picture Sudip Kafle · Feb 15, 2013 · Viewed 36.7k times · Source

I am trying to get the HTML content of child node with lxml and xpath in Python. As shown in code below, I want to find the html content of the each of product nodes. Does it have any methods like product.html?

productGrids = tree.xpath("//div[@class='name']/parent::*")
for product in productGrids:
    print #html content of product

Answer

Walty Yeung picture Walty Yeung · Feb 15, 2013
from lxml import etree
print(etree.tostring(root, pretty_print=True))

you may see more examples here: http://lxml.de/tutorial.html