2017-03-20 38 views
-2

現在我在工作中發現問題。 我需要http://air4thai.pcd.go.th/services/getAQI_XML.php?region=2 XML數據我想解析XML並輸出到CSVXML到CSV合作項目

和IM新手在Python 現在IM測試編碼解析PM10這樣

import xml.etree.ElementTree as ET 
import requests 
import os 
Air4thaiURL = 'http://air4thai.pcd.go.th/services/getAQI_XML.php?region=2' 
resp = requests.get(Air4thaiURL) 
msg = resp.content 
tree = ET.fromstring(msg) 
for station in tree.findall('.//station/LastUpdate'): 
print ('{}'.format(
station.get('PM10'))) 

,反而會導致這樣的

無 無 無 無 無 無 無 無 無 無 無 無 無 無 無 無 無

所以,它在價值/ IM顯示嘗試的類型必須:■它的結果

Traceback (most recent call last): 
    File "C:\Users\Gistda59\Desktop\Coop - Python Script\pm10_XML.py", line 11, in <module> 
    station.get('PM10'))) 
TypeError: non-empty format string passed to object.__format__ 

如何解決它與建議我從這個URL解析XML到CSV 非常感謝你幫助解決我的問題。

回答

0

是你想要這個東西:

import xml.etree.ElementTree as ET 
import requests 
import os 
Air4thaiURL = 'http://air4thai.pcd.go.th/services/getAQI_XML.php?region=2' 
resp = requests.get(Air4thaiURL) 
msg = resp.content 
tree = ET.fromstring(msg) 
for station in tree.findall('.//station/LastUpdate'): 
    if station.tag == 'LastUpdate': 
     for item in station: 
      if item.tag == 'PM10': 
       print(item.attrib)