Alex Rivera | Logout

How to preserve namespace information when parsing HTML with lxml?

Asked 2011-07-06T13:34:16.107
11
>>> from lxml.etree import HTML, tostring
>>> tostring(HTML('<fb:like>'))
'<html><body><like/></body></html>'

Note how the tag turns from <fb:like> to simply <like>.

This makes processing pages that incorporate XFBML with lxml much harder. (Same thing happens to <g:plusone></g:plusone>)

Any help is appreciated.

Edit
Report

1 Answer

1

Try adding the namespace prefix definitions that are missing. lxml will avoid the namespaces otherwise, supposedly to make it easier for you.

Most likely the sites you try to parse will not contain these namespace definitions, so you should add them.

Something like this: xmlns:adlcp="http://xxx/yy/zzz"

answered 2011-07-06T15:25:14.833

Your Answer