#-*-coding:utf-8-*-ImportRequestsImportJSONImportsysreload (SYS) sys.setdefaultencoding ('UTF8')defget_weather_data (): City='Shenzhen'URL='http://wthrcdn.etouch.cn/weather_mini?city='+City Text=requests.get (URL). Text Weather=json.loads (text)ifweather['desc'] =='OK': Print 'City:'+ weather['Data'][' City'] Print 'Temperature:'+ weather['Data']['Wendu'] +'℃' Print 'Cold:'+ weather['Data']['Ganmao'] Print 'Wind Direction:'+ weather['Data']['Forecast'][0]['Fengxiang'] Print 'Wind:'+ weather['Data']['Forecast'][0]['Fengli'] Print 'High Temperature:'+ weather['Data']['Forecast'][0][' High'] Print 'Low Temperature:'+ weather['Data']['Forecast'][0][' Low'] Print 'weather:'+ weather['Data']['Forecast'][0]['type'] Print 'Date:'+ weather['Data']['Forecast'][0]['Date'] Print '*'* 50 whileTrue:key= Raw_input ('show weather conditions for the next four days? (y/n)') ifKey = ='Y': Break elifKey = ='N': Exit () forIinchRange (1, 5): Print 'Date:'+ weather['Data']['Forecast'][i]['Date'] Print 'Wind Direction:'+ weather['Data']['Forecast'][i]['Fengxiang'] Print 'Wind:'+ weather['Data']['Forecast'][i]['Fengli'] Print 'High Temperature:'+ weather['Data']['Forecast'][i][' High'] Print 'Low Temperature:'+ weather['Data']['Forecast'][i][' Low'] Print 'weather:'+ weather['Data']['Forecast'][i]['type'] Print('-'* 50) Else: Print 'The city you entered is wrong, or the city you entered is not included in the weather center. ' if __name__=='__main__': Get_weather_data ()
Example--Query weather (convert data to JSON format)