Created
May 14, 2018 13:40
-
-
Save kimthostrup/59289d1dcb9a5ce03a30c3d4ecdcf26d to your computer and use it in GitHub Desktop.
example for parsing xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import xml.etree.ElementTree as ET | |
import urllib.request as MYURL | |
# tree = ET.parse('song.xml') | |
# rss = tree.getroot() | |
# for element in rss.findall('.channel/song'): | |
# print(element.attrib.get('sname')) | |
# print(element.findtext('title')) | |
# print(element.findtext('singer')) | |
kmaURL = "http://www.kma.go.kr/weather/forecast/mid-term-rss3.jsp?stnId=109" | |
response = MYURL.urlopen(kmaURL) | |
data = response.read().decode('UTF-8') | |
rss = ET.fromstring(data) | |
weathers = [] | |
for element in rss.findall('.//location'): | |
city = element.findtext('city') | |
datas = [] | |
for data in element.findall('.data'): | |
time = data.findtext('tmEf') | |
weather = data.findtext('wf') | |
minCel = data.findtext('tmn') | |
maxCel = data.findtext('tmx') | |
reliability = data.findtext('reliability') | |
datas.append({"time":time, "weather":weather, "minCel":minCel, "maxCel":maxCel, "reliability":reliability}) | |
weathers.append({"city":city, "data":datas}) | |
for n in weathers: | |
print('=' * 30) | |
print(n["city"]) | |
print('=' * 30) | |
for m in n["data"]: | |
print("시간: ", m["time"]) | |
print("날씨: ", m["weather"]) | |
print("최저온도: ", m["minCel"]) | |
print("최고온도: ", m["maxCel"]) | |
print("신뢰도: ", m["reliability"]) | |
print('-' * 30) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment