[英]What m i doing wrong in this code i have been stuck at
I'm trying to make a request and parse the current btc price.. 我正在尝试发出请求并解析当前的btc价格。
my code 我的代码
import requests
import json
r = requests.get('https://blockchain.info/ticker')
k = r.text
Rate = json.loads(k)
print(Rate["last"])
and the output is 输出是
Traceback (most recent call last):
File "wifi2.py", line 14, in <module>
print(Rate["last"])
KeyError: 'last'
how can i fix it? 我该如何解决? thanks
谢谢
This is a sample entry from the URL you provided: 这是您提供的URL中的示例条目:
"USD" : {"15m" : 11237.41, "last" : 11237.41, "buy" : 11237.41, "sell" : 11237.41, "symbol" : "$"}
They Key is the shorthand for the currency, and it's value is another dictionary, such that: 它们Key是货币的简写,其值是另一本词典,例如:
'USD' -> {..., "last": 11237.41, ...}
To get the value of the last key of the inner dictionary, you need to do for example: 要获取内部字典的最后一个键的值,您需要执行以下操作:
print(Rate["USD"]["last"])
This returns the dictionary associated with the first key, then the value associated with the last key of the inner dictionary. 这将返回与第一个键关联的词典,然后返回与内部词典的最后一个键关联的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.