带有命名空间的Python lxml iterfind,但prefix = None [英] Python lxml iterfind w/ namespace but prefix=None

查看:315
本文介绍了带有命名空间的Python lxml iterfind,但prefix = None的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想对具有名称空间但没有前缀的元素执行iterfind().我想打电话

I want to perform iterfind() for elements which have a namespace but no prefix. I'd like to call

iterfind([tagname])iterfind([tagname], [namespace dict])

我不在乎每次都按以下步骤输入标签:

I don't care to enter the tag as follows every time:

"{%s}tagname" % tree.nsmap[None]

详细信息

我正在处理来自Google API的xml响应.根节点定义了几个名称空间,其中包括一个没有前缀的名称空间:xmlns="http://www.w3.org/2005/Atom"

I'm running through an xml response from a Google API. The root node defines several namespaces, including one for which there is no prefix: xmlns="http://www.w3.org/2005/Atom"

当我尝试搜索自己的etree时,似乎所有行为都像我期望的带有前缀的元素一样.例如:

It looks as though when I try to search through my etree, everything behaves as I would expect for elements with a prefix. e.g.:

>>> for x in root.iterfind('dxp:segment'): print x
...
<Element {http://schemas.google.com/analytics/2009}segment at 0x1211b98>
<Element {http://schemas.google.com/analytics/2009}segment at 0x1211d78>
<Element {http://schemas.google.com/analytics/2009}segment at 0x1211a08>
>>>

但是当我尝试搜索不带前缀的内容时,搜索不会自动为root.nsmap[None]添加名称空间.例如:

But when I try to search for something without a prefix, the search doesn't automatically add the namespace for root.nsmap[None]. e.g.:

>>> for x in root.iterfind('entry'): print x
...
>>>

即使我尝试将名称空间映射作为iterfind的可选参数,它也不会附加名称空间.

Even if I try to throw the namespace map in as the optional argument for iterfind, It won't attach the namespace.

推荐答案

尝试一下:

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

有关更多信息:请阅读文档: http://lxml.de/tutorial.html#namespaces

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

(如果要使用xpath,也会发生同样的事情)

(same thing goes if you want to use xpath)

在文档中使用什么前缀(如果有的话)并不重要,这是关于您指定元素的完全限定名称,或者使用大括号表示法将其完整写成URI或使用映射的前缀到URI.

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.

这篇关于带有命名空间的Python lxml iterfind,但prefix = None的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆