반응형

 

 

OpenWeatherMap에서 제공하는 서울의 날씨 데이터를 JSON으로 받아와서 파이썬을 사용해서 파싱을 해봤습니다.

우선 전체 JSON 데이터의 형태입니다. 이 중에 몇 가지만 파싱해서 보기좋게 출력을 해보았습니다.

 

{

"coord":{"lon":126.98,"lat":37.57},

"sys":{"message":0.0103,"country":"KR","sunrise":1429735588,"sunset":1429784064},

"weather":[{"id":801,"main":"Clouds","description":"few clouds","icon":"02d"}],

"base":"stations",

"main":{"temp":288.243,"temp_min":288.243,"temp_max":288.243,"pressure":1009.39,"sea_level":1033.08,"grnd_level":1009.39,"humidity":26},

"wind":{"speed":4.45,"deg":234.006},

"clouds":{"all":24},

"dt":1429779813,

"id":1835848,

"name":"Seoul",

"cod":200

}

 

실행한 결과입니다.

 

import urllib

import json

import datetime

 

url = 'http://api.openweathermap.org/data/2.5/weather?q=Seoul,kr'

u = urllib.urlopen(url)

data = u.read()

print data

 

j = json.loads(data)

 

 

name = j["name"]

print "City name:"

print name

print "\n"

 

dt = j["dt"]

print "Data receiving time:"

print datetime.datetime.fromtimestamp(int(dt)).strftime('%Y-%m-%d %H:%M:%S')

print "\n"

 

main = j["main"]

temp = main["temp"]

print "temperature:"

print int(temp)-273.15

print "\n"

 

weather = j["weather"]

main = weather[0]

print main["main"]

print main["description"]

 

 

참고

http://openweathermap.org/weather-data#current

http://stackoverflow.com/questions/3682748/converting-unix-timestamp-string-to-readable-date-in-python

반응형

문제 발생시 지나치지 마시고 댓글 남겨주시면 가능한 빨리 답장드립니다.

도움이 되셨다면 토스아이디로 후원해주세요.
https://toss.me/momo2024


제가 쓴 책도 한번 검토해보세요 ^^

+ Recent posts