如何正确解析此XML? Python - ElementTree

时间:2015-12-05 19:04:12

标签: python xml xml-parsing elementtree

我在解析此XML时遇到问题。我对python很新,所以我可能不理解一个概念,或者我可能错过了一步。

XML块:

<result created="2015-12-05T12:46:00-06:00" host="www.systemmonitor.us"     status="OK">
<items>
 <client>
  <clientid>67300</clientid>
  <name>
    <![CDATA[ ACME Company ]]>
  </name>
<site>
  <siteid>85663</siteid>
  <name>
    <![CDATA[ Los Angeles ]]>
  </name>
  <workstations/>
  <servers>
    <server>
      <id>597207</id>
      <name>
        <![CDATA[ SERVER1 ]]>
      </name>
      <offline>
        <description>
           <![CDATA[ OFFLINE - MAINTENANCE MODE ]]>
        </description>
        <startdate>2015-11-25</startdate>
        <starttime>01:40:07</starttime>
      </offline>
     </server>
     <server>
      <id>2252213</id>
        <name>
          <![CDATA[ SERVER2 ]]>
        </name>
        <overdue>
         <description>
           <![CDATA[ Overdue ]]>
         </description>
         <startdate>2015-11-25</startdate>
         <starttime>01:57:40</starttime>
        </overdue>
      </server>
    </servers>
  </site>
</client>

我需要提取某些元素,以便将这些元素推送到我们的CRM系统中。

这是我的代码:

import requests
import xml.etree.ElementTree as ET

url = "https://www.systemmonitor.us/api/"
querystring = {"apikey":"SUPERSECRETAPIKEY","service":"list_failing_checks"}

response = requests.request("GET", url, params=querystring)
msg = response.text
tree = ET.ElementTree(ET.fromstring(msg))

client_ids = tree.find('clientid')

print client_ids

如果我尝试从msg.find('clientid')获取client_id,它只返回一个整数。 144.(我假设它是在xml中找到它的次数。)如果我使用tree.find('clientid')我得到[]。我还没有尝试迭代数据,因为我似乎无法找到它。

我想我已经尝试了所有可以找到和想到的其他组合,但我无法解决这个问题。 find(),findall()等。我的大脑因在桌子上猛击它而受伤。

我需要隔离clientid,name,overdue / description。

有人可以解释一下我做错了什么或我错过了什么?谢谢。

1 个答案:

答案 0 :(得分:1)

你必须提供完整的路径:

client_ids = tree.find('items/client/clientid')

或搜索所有出现的:

client_ids = tree.findall('.//clientid')

要获取元素的内容,例如

for client_id in client_ids:
    print client_id.text
相关问题