0

所以我正在使用 python 访问 poloniex API,这是我的代码:

from poloniex import Poloniex
import krakenex
import threading
import pprint
import urllib.request
import json
####POLONIEX####

#FUNCTIONS

polo = Poloniex()

def BTC_USDT_LAST_POLONIEX():
  polo = Poloniex()
  threading.Timer(1.0, BTC_USDT_LAST_POLONIEX).start() # called every minute
  print("BTC Last Price = " + (polo('returnTicker')['USDT_BTC']['last']))

def POLONIEX_ASSET_LIST():
    pprint.pprint(sorted(list(polo('returnTicker'))))

到目前为止一切正常,我想避免使用 urllib 作为将 http 请求转换为列表的痛苦。我正在尝试访问订单簿,但出现以下错误:

>>> polo('returnOrderBook')
Traceback (most recent call last):
  File "<pyshell#27>", line 1, in <module>
    polo('returnOrderBook')
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/poloniex/retry.py", line 15, in wrapped
    return function(*args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/poloniex/__init__.py", line 183, in __call__
    return self.parseJson(ret.text)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/poloniex/__init__.py", line 197, in parseJson
    raise PoloniexError(jsonout['error'])
poloniex.PoloniexError: Please specify a currency pair.

我试过指定货币对,但不知道如何插入。

4

1 回答 1

0

重写您的代码并使用requests模块而不是urllib

import requests
ret = requests.get('http://poloniex.com/public?command=returnOrderBook&currencyPair=BTC_BCN').json()
print ret

>>> {u'bids': [[u'0.00000034', 20629605.566027], [u'0.00000033', 43382683.465305], [u'0.00000032', 70007976.087993], [u'0.00000031', 49571221.248027], [u'0.00000030', 77520227.415484], [u'0.00000029', 46037827.046996], [u'0.00000028', 26267440.401662], [u'0.00000027', 22511987.85933], [u'0.00000026', 18885378.040015], [u'0.00000025', 13313109.292994], [u'0.00000024', 6243527.5236432], [u'0.00000023', 7504850.7832509], [u'0.00000022', 8443683.7997507], [u'0.00000021', 8996262.9826951], [u'0.00000020', 24601532.006268], [u'0.00000019', 26853346.478659], [u'0.00000018', 6027262.24889 etc....
于 2018-10-02T05:21:05.380 回答