-1

图片

我需要获取 HTTP 结果,特别是带有 BTC、ETH 和 USD 价格的部分(在图像中最后转换的情况下),然后将其保存为 python 中的变量,以便我可以在应用程序中显示价格。IDE:Pycharm Professional 操作系统:Windows 10 我也可以将 URL 的 GET 请求放入 python 脚本中,我只是不知道如何将 HTTP 结果缩小到 : 和 之间的数字,然后保存作为要显示给用户的变量,我需要能够保存来自 get 请求的 http 返回

 },
  "last": 46775.88,
  "volume": 410.2487549,
  "converted_last": {
    "btc": 0.99807622,
    "eth": 11.872556,
    "usd": 46893
  },

并将其缩小为仅 BTC ETH 和 USD,然后将其保存为变量以显示给用户。

4

1 回答 1

1

您需要做的是获取响应并将 json 转换为字典(或对象)。然后您可以将它们作为属性访问。我不确定您使用什么库来发出请求,但是对于请求,它看起来像:

import requests

# make the request
response = requests.get('https://api.coingecko.com/api/v3/coins/bitcoin')
# get the dictionary from the response
json = response.json()
# narrow down the information needed from the response
tickers = json['tickers']
print(tickers[0]['converted_last']['btc'])
于 2021-12-21T02:00:09.150 回答