找不到xml元素

2024-06-16 22:54:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试用xml.etree.ElementTree文件(我不能移动到lxml)但是我一直不能。你知道吗

XML:https://pastebin.com/yJqAW0L0

<GetResponse xmlns="http://mywebsite.com/myservice/">
    <AutoScalingGroup>
        <AutoScalingGroupName>foo</AutoScalingGroupName>
        <AttributeValuePair>
            <Attribute>owner</Attribute>
            <Value>bob</Value>
        </AttributeValuePair>
    </AutoScalingGroup>
</GetResponse>

我试过了

import xml.etree.ElementTree as ET

ET.register_namespace('', "http://mywebsite.com/myservice/")
NSMAP = {'service':'http://mywebsite.com/myservice/'}
tree = ET.fromstring(page) # This is where i grab the xml from
autoscalingGroups = tree.findall('.//service:AutoScalingGroup', namespaces = NSMAP)
for asg in autoscalingGroups:
    name = asg.findtext('.//service:AutoScalingGroupName', namespaces = NSMAP, default = "Default asg name")
    print "asg name: " + str(name)

这没有任何回报,我正在努力寻找原因。你知道吗

1)如何获得“foo”? 2) 我怎么得到“鲍勃”? 我使用了错误的xmlxpath吗?你知道吗


Tags: namecomhttpmyservicexmletetree