0

我从 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模块方法更好吗?

还是应该使用fixeriopython 模块?

PS =我正在使用python 2.7

4

2 回答 2

1

输出的快速打印显示 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': '#

于 2018-08-15T15:07:18.057 回答
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": "#################################################################################################################################"
}

所以密钥不存在。

于 2018-08-15T15:07:46.600 回答