[英]How to find element attribute using lxml
Suppose I have the the following xml:假设我有以下 xml:
<package xmlns="http://example/namespace">
<rating system="au-oflc">PG</rating>
...
</package>
To get the text of an element in the above, I am doing the following:要获取上述元素的文本,我正在执行以下操作:
from lxml import entree
f = open('/Users/David/Desktop/metadata.xml')
metadata_contents = f.read()
node = etree.fromstring(metadata_contents)
rating = node.xpath('//t:rating/text()', namespaces = {'t':'http://example/namespace'})
>>> rating
['PG']
How would I get the value "au-oflc" ?我将如何获得值“au-oflc”?
You need to retrieve the node itself, not its text:您需要检索节点本身,而不是其文本:
rating = node.xpath('//t:rating', namespaces = {'t':'http://example/namespace'})
print rating[0].attrib['system']
You can also access the attribute using XPath:您还可以使用 XPath 访问该属性:
system = node.xpath('//t:rating/@system', namespaces = {'t':'http://example/namespace'})
print system[0]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.