LoginSignup
20
15

More than 5 years have passed since last update.

Python3でjsonを返却するwebAPIにアクセスして結果を出力するまで

Last updated at Posted at 2015-08-12

今回は天気情報を返してくれるopenweathermap.orgのAPIにアクセス

import urllib.request
import json

url = 'http://api.openweathermap.org/data/2.5/weather?q=Tokyo,jp'
response = urllib.request.urlopen(url)
content = json.loads(response.read().decode('utf8'))
print(content)

出力結果

{
    "base": "stations", 
    "clouds": {
        "all": 40
    }, 
    "cod": 200, 
    "coord": {
        "lat": 35.69, 
        "lon": 139.69
    }, 
    "dt": 1439347788, 
    "id": 1850147, 
    "main": {
        "humidity": 70, 
        "pressure": 1005, 
        "temp": 305.28, 
        "temp_max": 312.04, 
        "temp_min": 299.82
    }, 
    "name": "Tokyo", 
    "sys": {
        "country": "JP", 
        "id": 7619, 
        "message": 0.014, 
        "sunrise": 1439323035, 
        "sunset": 1439372096, 
        "type": 1
    }, 
    "visibility": 10000, 
    "weather": [
        {
            "description": "scattered clouds", 
            "icon": "03d", 
            "id": 802, 
            "main": "Clouds"
        }
    ], 
    "wind": {
        "deg": 110, 
        "speed": 2.6
    }
}
20
15
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
20
15