Python lxml iterfind w/ namespace but prefix=None

风格不统一 提交于 2019-12-03 14:41:22

Try this:

for x in root.iterfind('{http://www.w3.org/2005/Atom}entry'):
    print x

For more information: read the docs: http://lxml.de/tutorial.html#namespaces

If you do not want to type that, and you want to provide a namespace map, you always have to use a prefix, like this for example:

nsmap = {'atom': 'http://www.w3.org/2005/Atom'}
for x in root.iterfind('atom:entry', namespaces=nsmap):
    print x

(same thing goes if you want to use xpath)

What prefix is used in the document, if any, is not important, it's about you specifying the fully qualified name of the element, either writing it out complete with URI using the curly bracket notation, or using a prefix that is mapped to a URI.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!