Bye Bye Moore

PoCソルジャーな零細事業主が作業メモを残すブログ

RequestsをつかってPythonで外部WEBに接続する その4:最初からjsonで取得する

最初の奴ではtextを一々jsonにパースしてましたが……
デフォでjsonを出力する奴があるんですね。

import requests

yourappkey = {{YOURAPPKEY}}
url = 'http://api.openweathermap.org/data/2.5/weather?id=1850147&appid={0}'.format(yourappkey)
r = requests.get()

dataset = r.json()
"""
{'coord': {'lon': 139.69, 'lat': 35.69}, 'weather': [{'id': 741, 'main': 'Fog', 'description': 'fog', 'icon': '50n'}], 'base': 'stations', 'main': {'temp': 275.01, 'pressure': 1022, 'humidity': 81, 'temp_min': 272.15, 'temp_max': 279.15}, 'visibility': 10000, 'wind': {'speed': 0.5, 'deg': 130}, 'clouds': {'all': 20}, 'dt': 1519394400, 'sys': {'type': 1, 'id': 7619, 'message': 0.0085, 'country': 'JP', 'sunrise': 1519334324, 'sunset': 1519374652}, 'id': 1850147, 'name': 'Tokyo', 'cod': 200}
"""

dataset["coord"]
# {'lon': 139.69, 'lat': 35.69}

dataset["coord"]["lon"]
# 139.69