简体   繁体   English

我在这段代码中做错了什么

[英]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.

相关问题 我对这个网页抓取代码做错了什么? - What I'm doing wrong with this webscraping code? 将Python代码实现为Javascript时,我在做什么错? - What i'm doing wrong when implementing Python code to Javascript? Python 错误? 我做错了什么? - Python Bug? What I'm doing wrong? 我是 Jupyter 的新手,不知道我在做什么有什么问题 - I'm brand new to Jupyter and have no idea what is wrong with what I am doing 我是 python 的新手,我觉得我在很长一段时间内都在做这段代码,但我坚持下一步该做什么 - I'm new to python and I feel like I am doing this code the long way but I am stuck at what to do next Pyttsx,我无法更改代码中的性别……,我做错了什么? - Pyttsx, I can't change the gender in the code…, what i'm doing wrong? 我在做什么错,我不确定如何修复我的代码 - What am I doing wrong, I'm not sure how to fix my code 我要知道我在做什么 - I need to know what I'm doing wrong 有人可以向我解释此Python代码中我在做什么错,并帮我弄清楚吗? - Could someone explain to me what I'm doing wrong in this Python Code and help me figure it out? 我正在做一个代码,但我没有看到我做错了什么。 我不断收到此错误'ValueError:int()的无效文字,基数为10:'2a' - i'm doing a code but i don't see what i'm doing wrong. i keep getting this error 'ValueError: invalid literal for int() with base 10: '2a'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM