使用Frankfurter API和Python虚拟环境时出现404错误

问题描述 投票:0回答:1

我正在 Visual Studio Code 中用 Python 制作货币转换器脚本,并且使用法兰克福 API,它在几天前一直工作正常,直到现在每次我尝试获取 API 时都会收到 404 错误:

>     response = requests.get(f"https://api.frankfurter.app/latest?amount={amount}&from={CF}&to={CT}")
>     print("API Response:", response.json())
>     print(response.status_code)

输出:

API Response: {'message': 'not found'}
404
Traceback (most recent call last):
  File "C:\Users\OMEN\Visual Studio\RTCurrencyConverter\Converter.py", line 59, in <module>
    print(f"{amount} {CF} is {response.json()['rates'][CT]} {CT}")
KeyError: 'rates'

对我来说,虚拟环境总体上非常不一致,我一直在尝试删除并重新创建环境,希望能够修复某些问题,但无济于事。

python python-3.x visual-studio-code python-venv
1个回答
0
投票

我已经尝试过该 API,它按预期工作,而且来自 Python3 的请求似乎也没有问题。下面我展示了一些可能存在问题的情况。

以下是正确的用法和正确的反应。

https://api.frankfurter.app/latest?amount=2&from=USD&to=GBP

{"amount":2.0,"base":"USD","date":"2024-07-30","rates":{"GBP":1.5569}}

这不是正确的用法。请参阅

from
参数而不是我写的 USD
UST
得到了 404,与您得到的响应相同:

https://api.frankfurter.app/latest?amount=2&from=UST&to=GBP

404 {"message":"not found"}

或者如果您的

CF
CT
参数可能是 None ,如下例所示。 我发送了空的
to
参数:

https://api.frankfurter.app/latest?amount=2&from=USD&to=
404 {"message":"not found"}

在发送请求之前记录您的参数并查看如下代码:

request_url = f"https://api.frankfurter.app/latest?amount={amount}&from={CF}&to={CT}"
print(f"Trying the request \n {request_url}")
response = requests.get(request_url)
print("API Response:", response.json())
print(response.status_code)
© www.soinside.com 2019 - 2024. All rights reserved.