退屈なことはPythonにやらせようという本でAPIを使用して天気情報を取得するページなのですが、エラーが出てしまい解決でできないので質問させていただきます。
Python
1 2#! python3 3# quickWeather.py - コマンドラインに指定した地名の天気予報を表示する 4 5import json, requests, sys 6 7# コマンドライン引数から地名を組み立てる 8if len(sys.argv) < 2: 9 print('Usage: quickWeather.py location') 10 sys.exit() 11location = ' '.join(sys.argv[1:]) 12 13# openweathermap.orgから取得したAPIキーを定義しておく 14APPID="自分のAPI" 15 16# OpenWeatherMap.orgのAPIからJSONデータをダウンロードする 17url ='http://api.openweathermap.org/data/2.5/forecast/daily?q={}&cnt=3&appid={}'.format(location, APPID) 18response = requests.get(url) 19response.raise_for_status() 20 21# JSONデータからPython変数に読み込む 22weather_data = json.loads(response.text) 23# 天気予報の情報を表示する 24w = weather_data['list'] # ❶ 25print('{}の現在の天気:'.format(location)) 26print(w[0]['weather'][0]['main'], '-', w[0]['weather'][0]['description']) 27print() 28print('明日:') 29print(w[1]['weather'][0]['main'], '-', w[1]['weather'][0]['description']) 30print() 31print('明後日:') 32print(w[2]['weather'][0]['main'], '-', w[2]['weather'][0]['description'])
エラーメッセージ
(base) MacBook-Pro:test username$ python3 quickWeather.py San Francisco, CA Traceback (most recent call last): File "quickWeather.py", line 19, in <module> response.raise_for_status() File "/Users/bananafish/opt/anaconda3/lib/python3.8/site-packages/requests/models.py", line 941, in raise_for_status raise HTTPError(http_error_msg, response=self) requests.exceptions.HTTPError: 401 Client Error: Unauthorized for url: http://api.openweathermap.org/data/2.5/forecast/daily?q=San%20Francisco,%20CA&cnt=3&appid=自分のAPI
このエラーはどのように解決すれば良いのでしょうか?
OpenWeatherサイトにサインアップはしておりAPIキーは問題ないはずです。
詳しい方よろしくお願いします。
これかな?
https://teratail.com/questions/107349
ご回答ありがとうございます。
リンク先の回答はすでに閲覧しておりましたが、無料アカウントの意味がよくわかりませんでした。
そもそもこちらはOpenWeatherの仕様変更でお金を払わないと目的のAPIが使用できなくなってしまったということでしょうか?
https://openweathermap.org/api
を見て、欲しいデータが「free」に含まれるものか、確認してください

回答1件
あなたの回答
tips
プレビュー