2012-11-27 24 views
0

我寫了一個veeery簡單腳本,在這裏我可以下載特定城市(例如倫敦)的天氣預報。它從今天開始下載7天的天氣,但我對這種JSON格式感到困惑,請問有人能給我一些關於如何將天氣預報件分成幾天的建議,我的意思是,如何做到這一點,以便我的腳本將打印:使用Python獲取天氣狀況 - 我對JSON格式感到困惑

Day 1st (today + date), weather: .... 
Day 2nd (date), weather: ... 

我的代碼:

#!/usr/bin/env python 

import urllib2, json 
def getWeatherCondition(city) : 
    try : 
     url = "http://openweathermap.org/data/2.1/forecast/city?q=" 
     url += city 
     req = urllib2.Request(url) 
     response=urllib2.urlopen(req) 
    except Exception : 
     print("Sth went wrong") 
    return response.read() 

if __name__ == "__main__": 
    print (getWeatherCondition("London")) 

我知道如何處理其他的事情,但我有天通過其分割問題:(

+0

您可能會發現有用的[我的GitHub Python項目PyOWM](https://github.com/csparpa/pyowm),它是一個簡單的面向對象的客戶端打包在Open Weather Map Web API周圍。 – csparpa

回答