python
1import urllib.request 2import json 3 4# 天気クラス 5class Weather: 6 def __init__(self, data): 7 # 日付 8 self.date = data["date"] 9 10 #テロップ 11 self.terop = data["terop"] 12 13 # 最高気温 14 if data["temperature"]["max"] is not None: 15 self.temperature_max = "{0}度".format(data["temperature"]["max"]["celsius"]) 16 else: 17 self.temperature_max = "--" 18 19 # 最低気温 20 if data["temperature"]["min"] is not None: 21 self.temperature_min = "{0}度".format(data["temperature"]["min"]["celsius"]) 22 else: 23 self.temperature_min = "--" 24 25 def print(self): 26 print(self.date) 27 print(" " + self.telop) 28 print(" 最高気温: {0}".format(self.temperature_max)) 29 print(" 最低気温: {0}".format(self.temperature_min)) 30 31# Weather Hacks URL 32URL = "http://weather.livedoor.com/forecast/webservice/json/v1?city={0}" 33 34# 町コード:大阪市 35ID = 271004 36 37# リクエスト実行 38req = urllib.request.Request(URL.format(ID)) 39with urllib.request.urlopen(req) as res: 40 # レスポンス結果 41 data = json.load(res) 42 print(data["title"]) 43 44 # 天気結果の取得 45 weathers = [] 46 for forecast in data["forecasts"]: 47 weathers.append(Weather(forecast)) 48 49 # 天気結果の出力 50 for item in weathers: 51 item.print()
リクエスト実行
から数えて2行目のwith urllib.request.urlopen(req) as res:の部分においてHTTP Error 404: Not Foundとなるのでこのコードの部分をどう書きかえたらよいでしょうか?
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/09/24 13:02