简体   繁体   English

如何使用lxml在AWS响应中查找元素(命名空间问题?)

[英]How to lookup element in AWS response with lxml (namespace issue?)

I'm trying to use lxml to read a response from the AWS REST API but not having any luck. 我正在尝试使用lxml从AWS REST API读取响应,但是没有任何运气。 I can easily parse the response and print it, but none of the find or xpath functions find anything. 我可以轻松地解析并打印响应,但是findxpath函数都找不到任何东西。 For example, take this document fragment: 例如,获取以下文档片段:

<DistributionConfig xmlns="http://cloudfront.amazonaws.com/doc/2013-11-11/">
  <CallerReference>e6d6909d-f1ed-47f1-83d9-290acf10f324</CallerReference>
  <Aliases>
    <Quantity>1</Quantity>
    <Items>

And this code: 这段代码:

from lxml import etree
root = etree.XML( ... )
node = root.find( 'Quantity' ) 

node is always None . node始终为None I've tried a variety of xpaths like //Quanity , .//Quantity , and also the xpath function, but can't find anything. 我已经尝试了各种xpath,例如//Quanity ,. //Quanity .//Quantity以及xpath函数,但找不到任何东西。

How do I use this library on this type of document? 如何在此类文档上使用此库?

Seems you will need to supply the namespace of the element as well: 似乎您还需要提供元素的名称空间:

>>> root.find('.//aws:Quantity', namespaces={'aws': 'http://cloudfront.amazonaws.com/doc/2013-11-11/'})
<Element {http://cloudfront.amazonaws.com/doc/2013-11-11/}Quantity at 0xb6c16aa4>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM