openweather と json 形式を使用して、Python で気象条件を取得する簡単なコードを見つけて変更しました。しかし、問題があります。都市が間違っているとどのように言えますか?
つまり、間違った存在しない都市を通過しても、 read は常に答えを返します (「空の応答」などはありません)。
私が話していることを確認するには、以下のコードを参照してください。
#!/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!