python - lxml modify tags prevent -
how prevent lxml modify tags
from lxml import etree lxml.html.soupparser import fromstring html = '<iframe width="560" height="315" src="" frameborder="0" allowfullscreen></iframe>' root = fromstring(html) print etree.tostring(root,encoding='utf-8')
it prints short version of tag
'<iframe width="560" height="315" src="" frameborder="0" allowfullscreen/>'
how prevent this? needed output
'<iframe width="560" height="315" src="" frameborder="0" allowfullscreen></iframe>'
?
use tostring()
method="html"
:
print etree.tostring(root.find('iframe'), encoding='utf-8', method="html")
demo:
>>> lxml import etree >>> lxml.html.soupparser import fromstring >>> >>> html = '<iframe width="560" height="315" src="" frameborder="0" allowfullscreen></iframe>' >>> root = fromstring(html) >>> print etree.tostring(root.find('iframe'), encoding='utf-8', method="html") <iframe allowfullscreen="allowfullscreen" frameborder="0" height="315" src="" width="560"></iframe>
Comments
Post a Comment