我有一个XML响应。我需要提取uri
属性,其中{{1}中{{1}中的local=1
标记为Connection
,或者简而言之,我需要Device
product=Plex Media Server
我是超级新人。我可以得到要打印的最后一个uri
this.is.what.i.want
结果:
<?xml version="1.0" encoding="UTF-8"?>
<MediaContainer size="5">
<Device name="Cast" product="Plex Media Server" productVersion="1.14.1.5490-33830be0b" platform="Android" platformVersion="8.0.0" device="SHIELD Android TV" clientIdentifier="" createdAt="" lastSeenAt="" provides="" owned="1" accessToken="" publicAddress="" httpsRequired="1" synced="0" relay="1" dnsRebindingProtection="0" publicAddressMatches="1" presence="1">
<Connection protocol="https" address="12.34.56.78" port="xxx" uri="this.is.what.i.want" local="1"/>
<Connection protocol="https" address="91.01.11.21" port="xxx" uri="https://111-111-111-111.863fffffffffffffflf.plex.direct:xxx" local="0"/>
</Device>
<Device name="TV" product="Plex for Samsung" productVersion="2.012" platform="Samsung" platformVersion="5.1" device="Samsung TV" clientIdentifier="xxx" createdAt="" lastSeenAt="" provides="client,player,pubsub-player" owned="1" publicAddress="" publicAddressMatches="0" presence="0" accessToken="">
<Connection protocol="https" address="xx.xx.xx.xx" port="xx" uri="https://222-222-222-222.863ffffffffffffffff.plex.direct:xxx" local="1"/>
<Connection protocol="https" address="xx.xx.xx.xx" port="xx" uri="https://333-444-555-666.863ffffffffffffffff.plex.direct:xxx" local="0"/>
</Device>
</MediaContainer>
但是,当我尝试添加一些限定词时,我的代码就会崩溃:
tree = ET.parse('test.xml')
root = tree.getroot()
for Connection in root.iterfind('Device/Connection'):
uri = Connection.get('uri')
print(uri)
结果:
https://333-444-555-666.863ffffffffffffffff.plex.direct:xxx
由于某种原因,代码总是打印最后的设备属性,而不是第一个。
感谢您的帮助。