KeyError ['date'] - Python fixer.io
Я начинаю заново с fixerio, у меня есть этот код:
import requests
import json
url = "http://api.fixer.io/latest?base=USD"
response = requests.get(url)
data = response.text
parsed = json.loads(data)
date = parsed["date"]
print("Date:", date, "\n")
rates = parsed["rates"]
for currency, rate in rates.items():
print(currency, "= USD", rate)
Каждый раз, когда я запускаю его, он выдает:
C:\usio>python fixerio.py
Traceback (most recent call last):
File "fixerio.py", line 9, in <module>
date = parsed["date"]
KeyError: 'date'
Проблема в том, что я не понимаю, как я могу "объявить" эту дату, я имею в виду, что ей явно не хватает какого-то объявления.
Кроме того, говоря о fixer.io
Вы думаете, лучше с requests
модульный подход?
Или если fixerio
модуль Python будет использоваться?
PS = я использую Python 2.7
2 ответа
Решение
Быстрая печать вывода показывает, что API сломан
import requests
import json
url = "http://api.fixer.io/latest?base=USD"
response = requests.get(url)
data = response.text
parsed = json.loads(data)
print(parsed)
{'0': '#######################################################################################################################################', '1': '#
Если вы идете в url
в вашем браузере вы можете увидеть, что они подняли уведомление
{
"0": "#################################################################################################################################",
"1": "# #",
"2": "# IMPORTANT - PLEASE UPDATE YOUR API ENDPOINT #",
"3": "# #",
"4": "# This API endpoint is deprecated and has now been shut down. To keep using the Fixer API, please update your integration #",
"5": "# to use the new Fixer API endpoint, designed as a simple drop-in replacement. #",
"6": "# You will be required to create an account at https://fixer.io and obtain an API access key. #",
"7": "# #",
"8": "# For more information on how to upgrade please visit our Github Tutorial at: https://github.com/fixerAPI/fixer#readme #",
"9": "# #",
"a": "#################################################################################################################################"
}
Так что ключ не существует.