如何使用Python读取JSON URL以创建有用的字典

时间:2017-01-30 07:23:10

标签: python json dictionary

我正在使用的数据可在此处找到 - JSON Information

我目前正在使用此代码读取数据,但结果对我来说很陌生,我不知道如何使用它:

import requests

site='http://www.bom.gov.au/fwo/IDN60801/IDN60801.95896.json'
r=requests.get(site)

print r.json()

这会输出一个庞大的字典?我不确定如何,但我希望能够从 JSON 网站检索到的是第一个" air_temp "值。

我确信这并不难,我只是n00b

感谢您的帮助!

以下是一些示例输出:

    {
   u'observations':{
      u'header':[
         {
            u'state_time_zone':u'NSW',
            u'time_zone':u'EDT',
            u'main_ID':u'IDN60800',
            u'product_name':u'Weather Observations',
            u'state':u'New South Wales',
            u'refresh_message':            u'Issued at  6:02            pm EDT Monday 30 January 2017',
            u'ID':u'IDN60801',
            u'name':u'Albury'
         }
      ],
      u'notice':[
         {
            u'copyright_url':            u'http://www.bom.gov.au/other/copyright.shtml',
            u'disclaimer_url':            u'http://www.bom.gov.au/other/disclaimer.shtml',
            u'copyright':u'Copyright Commonwealth of Australia 2017,
            Bureau of Meteorology. For more information see:            http:            //www.bom.gov.au/other/copyright.shtml http://www.bom.gov.au/other/disclaimer.shtml',
            u'feedback_url':            u'http://www.bom.gov.au/other/feedback'
         }
      ],
      u'data':[
         {
            u'swell_period':None,
            u'wind_dir':u'SW',
            u'lat':-36.1,
            u'cloud_oktas':2,
            u'gust_kt':16,
            u'history_product':u'IDN60801',
            u'local_date_time_full':u'20170130180000',
            u'cloud':u'Mostly clear',
            u'press_msl':1006.8,
            u'cloud_type':u'-',
            u'wind_spd_kmh':30,
            u'lon':147.0,
            u'swell_height':None,
            u'wmo':95896,
            u'press_qnh':1007.6,
            u'weather':u'-',
            u'wind_spd_kt':16,
            u'rain_trace':u'-',
            u'aifstime_utc':u'20170130070000',
            u'delta_t':13.1,
            u'press_tend':u'-',
            u'rel_hum':24,
            u'local_date_time':            u'30/06:00            pm',
            u'press':1006.8,
            u'vis_km':u'47',
            u'sea_state':u'-',
            u'air_temp':32.2,
            u'name':u'Albury',
            u'cloud_base_m':2500,
            u'cloud_type_id':None,
            u'gust_kmh':30,
            u'dewpt':9.1,
            u'swell_dir_worded':u'-',
            u'sort_order':0,
            u'apparent_t':26.3
         },
         {
            u'swell_period':None,
            u'wind_dir':u'WSW',
            u'lat':-36.1,
            u'cloud_oktas':None,
            u'gust_kt':25,
            u'history_product':u'IDN60801',
            u'local_date_time_full':u'20170130174300',
            u'cloud':u'-',
            u'press_msl':1006.7,
            u'cloud_type':u'-',
            u'wind_spd_kmh':28,
            u'lon':147.0,
            u'swell_height':None,
            u'wmo':95896,
            u'press_qnh':1007.5,
            u'weather':u'-',
            u'wind_spd_kt':15,
            u'rain_trace':u'0.0',
            u'aifstime_utc':u'20170130064300',
            u'delta_t':13.4,
            u'press_tend':u'-',
            u'rel_hum':23,
            u'local_date_time':            u'30/05:43            pm',
            u'press':1006.7,
            u'vis_km':u'10',
            u'sea_state':u'-',
            u'air_temp':32.4,
            u'name':u'Albury',
            u'cloud_base_m':None,
            u'cloud_type_id':None,
            u'gust_kmh':46,
            u'dewpt':8.6,
            u'swell_dir_worded':u'-',
            u'sort_order':1,
            u'apparent_t':26.8
         },

1 个答案:

答案 0 :(得分:3)

看一下python dictionary docs

提取所有温度:

>>> r=requests.get(site)
>>> data = r.json()
>>> allTemps = [item['air_temp'] for item in data['observations']['data']]
>>> print(allTemps)
[32.2, 32.4, 33.0, 36.6, 40.7, 41.2, 40.8, 39.8, 39.6, 40.6, 40.9, 40.4, 41.1, 40.5, 40.4, 41.2, 39.6, 40.6, 39.1, 39.1, 38.6, 38.6, 33.6, 31.5, 29.1, 26.6, 25.7, 23.5, 22.4, 20.2, 19.8, 17.2, 17.2, 17.3, 17.7, 18.1, 18.4, 18.8, 18.9, 20.2, 20.6, 21.0, 20.4, 21.7, 22.3, 23.5, 23.5, 23.0, 24.7, 27.9, 28.9, 28.1, 33.2, 34.2, 35.9, 36.7, 37.4, 37.5, 37.7, 37.5, 37.6, 37.7, 38.0, 37.1, 36.5, 36.6, 35.7, 34.9, 34.3, 32.9, 31.6, 30.1, 28.7, 27.1, 25.6, 24.3, 22.7, 21.2, 19.5, 17.9, 16.9, 17.7, 17.9, 18.0, 18.6, 18.9, 20.1, 20.1, 20.2, 20.9, 21.5, 21.0, 21.5, 22.6, 25.1, 24.5, 24.9, 26.1, 27.2, 29.8, 30.3, 32.1, 33.3, 34.3, 35.0, 35.4, 35.7, 36.0, 35.9, 35.6, 35.0, 35.7, 34.7, 35.0, 34.5, 34.1, 33.6, 32.9, 32.2, 31.3, 29.3, 27.1, 25.3, 23.6, 22.7, 20.9, 19.6, 18.4, 18.2, 18.1, 17.7, 18.2, 18.1, 18.9, 19.6, 19.8, 20.8, 21.7, 22.0, 22.6, 22.7, 23.7, 24.5, 24.2, 25.5, 26.8, 28.6, 29.3, 30.8, 31.8, 33.3, 34.1, 34.4]

只是第一个温度:

>>> firstTemp = data['observations']['data'][0]['air_temp']
>>> print(firstTemp)
32.2