【发布时间】:2017-09-30 09:40:37
【问题描述】:
我正在创建一个简单的程序,它从 Bitstamp.net API 获取“最后一个”值并使用它。请参阅下面的代码。
def getBitcoinPrice():
url = 'https://www.bitstamp.net/api/ticker/'
try:
r = requests.get(url)
priceFloat = float(json.loads(r.text)['last'])
return priceFloat
except requests.ConnectionError:
print("Error querying Bitstamp API")
os.system('say "The program broke."')
有时我会在 3 分钟后收到零星的 JSONDecodeError,有时会在几个小时后收到。我到处看了看,无法弄清楚。请参阅下面的错误。非常感谢任何帮助!
File "/Users/paulkaraffa/PycharmProjects/socialbitanalytics/actionEvaluator.py", line 70, in <module>
btcValues.append(getBitcoinPrice())
File "/Users/paulkaraffa/PycharmProjects/socialbitanalytics/actionEvaluator.py", line 26, in getBitcoinPrice
priceFloat = float(json.loads(r.text)['last'])
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/__init__.py", line 354, in loads
return _default_decoder.decode(s)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/decoder.py", line 357, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
【问题讨论】: