2013-02-17 43 views
0

我迷路了我想从python的url xml文件中提取一个值,我看到了那里有很多库,但无法取得一些成功。另一个xml python提取

<?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' 
+0

那你试试这么远吗? – oefe 2013-02-17 20:49:17

+0

然后,不存在ID为“75”的“状态”节点... – 2013-02-17 21:22:34

+0

我知道为什么现在,我使用浏览器进行检查,并且每次我重新编写id更改。有没有一种方法来检查变量IdleTime而不是 – user2052746 2013-02-17 21:40:31

回答

0

xml是值一条条您的数据,然后使用lxml的NG,你可以这样做:

​​

或者,您可以执行以下操作使用内置xml库:

import xml.etree.ElementTree as ET 

tree = ET.fromstring(xml) 
print tree.find('.//state[@id="75"]').attrib['service'] 
# urn:upnp-org:serviceId:XBMCState1 
+0

python是用于xbmc插件的有没有一个标准的方法,没有安装其他库或有没有办法将该库与Python脚本? – user2052746 2013-02-17 21:02:35

+0

@ user2052746使用Python的内置'xml'库进行编辑 – 2013-02-17 21:10:02

+0

这里我的代码:fefe – user2052746 2013-02-17 21:17:22