我迷路了,我想从 python 中的 url xml 文件中提取一个值,我看到那里有很多库,但无法取得一些成功。
<?xml version="1.0"?>
<root LoadTime="1360991134" DataVersion="991138826" UserData_DataVersion="991134040"
TimeStamp="1361131034" ZWaveStatus="1" LocalTime="2013-02-17 14:57:14 D">
<Device_Num_82 status="-1">
<states>
<state id="72" service="urn:upnp-org:serviceId:XBMCState1" variable="Port" value="80">
</state>
<state id="73" service="urn:upnp-org:serviceId:XBMCState1" variable="PingInterval"
value="180"></state>
<state id="74" service="urn:upnp-org:serviceId:XBMCState1" variable="PingStatus"
value="up"></state>
<state id="75" service="urn:upnp-org:serviceId:XBMCState1" variable="IdleTime"
value="program"></state>
<state id="76" service="urn:upnp-org:serviceId:XBMCState1" variable="PlayerStatus"
value="Video_end"></state>
<state id="77" service="urn:micasaverde-com:serviceId:HaDevice1" variable="CommFailure"
value="1"></state>
<state id="78" service="urn:micasaverde-com:serviceId:HaDevice1" variable="LastUpdate"
value="0"></state>
<state id="79" service="urn:micasaverde-com:serviceId:HaDevice1" variable="Configured"
value="0"></state>
</states>
<Jobs></Jobs>
<tooltip display="0"></tooltip>
</Device_Num_82>
</root>
我想要 id=75 的值(程序)
谢谢
这里的代码
import xml.etree.ElementTree as ET
import urllib.request
VERA = 'http://192.168.2.19:3480/data_request?id=status&output_format=xml&DeviceNum=82'
xml = urllib.request.urlopen(VERA).read()
tree = ET.fromstring(xml)
print (tree.find('.//state[@id="75"]').attrib['service'])
# urn:upnp-org:serviceId:XBMCState1
这里的错误
Traceback (most recent call last):
File "/Users/Michael/Desktop/test.py", line 8, in <module>
print (tree.find('.//state[@id="75"]').attrib['service'])
AttributeError: 'NoneType' object has no attribute 'attrib'