简体   繁体   English

如何使用lxml查找元素属性

[英]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']

georg 's answer assumes all rating elements will have a system tag. georg回答假设所有rating元素都有一个system标签。 if that is not necessarily the case, using rating[0].attrib.get('system') will avoid a KeyError.如果情况并非如此,则使用rating[0].attrib.get('system')将避免rating[0].attrib.get('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.

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