3
我找到並修改了簡單的代碼,以便使用openweather和json格式在Python中獲取天氣狀況。但我有一個問題 - 我怎麼說這個城市是不正確的?Python openweather,json - 通過城市名稱獲取天氣 - 我怎麼說這個城市是不正確的?
我的意思是,即使我通過一個錯誤的,不存在的城市,閱讀總是給出一個答案(theres沒有這樣的事情像'空迴應'或類似的東西)。
請參見下面的代碼,看看我說的是:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import urllib2, json
city = "etre4t5r5e4re" # the city name is incorrent
url = "http://openweathermap.org/data/2.1/forecast/city?q="
url += city
try :
request = urllib2.Request(url)
response = urllib2.urlopen(request)
except urllib2.HTTPError, e:
info = wx.MessageBox(u"Internet connection error", u"Error", wx.OK | wx.ICON_ERROR)
except urllib2.URLError, e:
info = wx.MessageBox(u"Internet connection error", u"Error", wx.OK | wx.ICON_ERROR)
except httplib.HTTPException, e:
info = wx.MessageBox(u"Internet connection error", u"Error", wx.OK | wx.ICON_ERROR)
except Exception:
info = wx.MessageBox(u"Error", u"Error", wx.OK | wx.ICON_ERROR)
weather = response.read()
if __name__ == '__main__':
print(weather) # it will show weather but thats not what I want for non-existing city!
的URL通過'http://openweathermap.org/data/2.1/forecast/city Q = etre4t5r5e4re'返回'內部服務器錯誤未定義指數:geonames_id'我。 –
當我試圖得到不正確的城市信息時,該服務給了我莫斯科。 – alexvassel
@NiclasNilsson:奇怪,因爲我實際上得到這個輸出:http://pastebin.com/aa1q6DBK沒有錯誤o_O – mazix