API 来自 get_buy_price() 的 Coinbase ValueError

API Coinbase ValueError from get_buy_price()

python 3.4 和 Coinbase V2 API


我正在做一些 BTC 数据分析,并试图不断向 ​​coinbase API 发出请求。当 运行 我的脚本时,它总是最终会在调用

时崩溃
r = client.get_spot_price()
r = client.get_buy_price()
r = client.get_sell_price()

不寻常的是脚本总是会在不同的时间崩溃。有时它会成功收集一个小时左右的数据然后崩溃,其他时候它会在 5 - 10 分钟后崩溃。

错误

    r = client.get_spot_price()
  File "/home/g/.local/lib/python3.4/site-packages/coinbase/wallet/client.py", line 191, in get_spot_price
    response = self._get('v2', 'prices', 'spot', data=params)
  File "/home/g/.local/lib/python3.4/site-packages/coinbase/wallet/client.py", line 129, in _get
    return self._request('get', *args, **kwargs)
  File "/home/g/.local/lib/python3.4/site-packages/coinbase/wallet/client.py", line 116, in _request
    return self._handle_response(response)
  File "/home/g/.local/lib/python3.4/site-packages/coinbase/wallet/client.py", line 125, in _handle_response
    raise build_api_error(response)
  File "/home/g/.local/lib/python3.4/site-packages/coinbase/wallet/error.py", line 49, in build_api_error
    blob = blob or response.json()
  File "/home/g/.local/lib/python3.4/site-packages/requests/models.py", line 812, in json
    return complexjson.loads(self.text, **kwargs)
  File "/usr/lib/python3.4/json/__init__.py", line 318, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3.4/json/decoder.py", line 343, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python3.4/json/decoder.py", line 361, in raw_decode
    raise ValueError(errmsg("Expecting value", s, err.value)) from None
ValueError: Expecting value: line 1 column 1 (char 0)

似乎是由于某些 json 解码而崩溃?

有谁知道为什么这只会在特定时间抛出错误?


我已经尝试了类似下面的方法来避免由于这个错误而崩溃:

#snap is tuple of data containing data from buy, sell , spot price
if not any(snap):                                                                                                 
    print('\n\n-----ENTRY ERROR---- Snap returned None \n\n')                                                     
    success = False                                                                                               
    return 

但它并没有起到作用

在您看来,有哪些处理此错误的好方法?


谢谢,非常感谢任何帮助!

对我来说,这可能与那个问题有关https://github.com/coinbase/coinbase-python/issues/15。这似乎实际上是一个 内部库错误 (因为代码确实 raise build_api_error(response) 证实了我的断言)。

问题可能与互联网连接有关?如果您的网络(或服务器出现故障),它可能无法检索到 JSON 文件或检索到一个空文件。但是,图书馆应该更清楚地通知您。

因此,它将尝试在 JSON 解码器中解码一个空文件,导致错误的原因。

一个临时的解决方法是用 try 语句来支撑您的代码,如果失败则重试。

您必须为其提供一种货币才能获得价格。

这是一个例子:

price = client.get_spot_price(currency_pair='XRP-USD')