我正在写一个小的python应用程序,它将更新我的投资组合livecoin.net网站. 我使用livecoin.net网站API以及coinmarketcap.com应用程序编程接口。当从coinmarketcap.comAPI,如果您转到它(https://api.coinmarketcap.com/v1/ticker/bitcoin/)显然是JSON,我会得到以下错误:
Traceback (most recent call last):
File "C:/Users/other/Desktop/livecoin.py", line 89, in <module>
cmcData = getCoinMarketCapData(balances)
File "C:/Users/other/Desktop/livecoin.py", line 22, in getCoinMarketCapData
cmcData = json.load(cmcResponse)
File "C:\Python27\lib\json\__init__.py", line 291, in load
**kw)
File "C:\Python27\lib\json\__init__.py", line 339, in loads
return _default_decoder.decode(s)
File "C:\Python27\lib\json\decoder.py", line 364, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Python27\lib\json\decoder.py", line 382, in raw_decode
raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded
正如我前面所说的,我得到的页面非常清楚地是JSON,所以我真的不知道为什么会发生这种情况。有人能帮我吗?你知道吗
我很清楚,这不是一个慈善代码工作的网站,但我很难办,这是我的计划Z。。。如果你愿意帮忙,甚至不需要为我写任何代码,只要试着告诉我哪里错了。谢谢:D
这是我的密码:
# -*- coding: utf-8 -*-
import httplib
import urllib
import json
import hashlib
import hmac
from collections import OrderedDict
def getCoinMarketCapData(currencies):
returns = {}
cmcUrl = "api.coinmarketcap.com"
cmcMethod = "/v1/ticker/"
conn = httplib.HTTPSConnection(cmcUrl)
for item in currencies:
print item['name'].split(" ")[0].lower();
print cmcMethod+(item['name'].split(" ")[0].lower())
conn.request("GET", cmcMethod+(item['name'].split(" ")[0].lower()))
cmcResponse = conn.getresponse()
cmcData = json.load(cmcResponse)
c = {"name":item['name'].split(" ")[0], "currency":item['symbol'], "price_usd":cmcData[0]['price_usd'], "price_btc":cmcData[0]['price_btc'], "d1h":cmcData[0]['percent_change_1h'], "d24h":cmcData[0]['percent_change_24h'], "d7d":cmcData[0]['percent_change_7d'], "value":0.0}
returns[item['symbol']] = c
conn.close()
return returns
def getData(dataDict, method, server, key, secret):
encoded_data = urllib.urlencode(dataDict)
sign = hmac.new(secret, msg=encoded_data, digestmod=hashlib.sha256).hexdigest().upper()
headers = {"Api-key":key, "Sign":sign}
conn = httplib.HTTPSConnection(server)
conn.request("GET", method + '?' + encoded_data, '', headers)
response = conn.getresponse()
data = json.load(response)
conn.close()
return data
def outputLine(key, value, prefix, suffix):
spaces = ""
pslen = len(prefix) + len(suffix)
key = key.upper()
key = " " + key + ": "
keylen = len(key)
x = 35-keylen-pslen
for count in range(x-len(str(value))):
spaces += " "
return key + prefix + value + suffix + spaces
server = "api.livecoin.net"
balancesMethod = "/payment/balances"
coinInfoMethod = "/info/coinInfo"
api_key = "gEuyw7k4WvAhdXUmG36zHDksDZGR3fvq"
secret_key = "D4aTtN6tPxBqqDG24PFZ1238CMektp33"
responses = []
names = []
balances = []
namesJSON = getData([], coinInfoMethod, server, api_key, secret_key)['info']
for name in namesJSON:
names.append({"name":name['name'], "symbol":name['symbol']})
data = OrderedDict([])
d = getData(data, balancesMethod, server, api_key, secret_key)
responses.append(d)
for currency in responses[0]:
if currency['value'] > 0.0 and currency['type'] == 'total':
for name in names:
if name['symbol'] == currency['currency']:
currency['name'] = name['name']
balances.append(currency)
ownedCoins = []
for balance in balances:
ownedCoins.append({"name":balance['name'], "symbol":balance['currency']})
print ownedCoins
cmcData = getCoinMarketCapData(balances)
for balance in balances:
try:
cmcData[balance['currency']]['value'] = balance['value']
except:
continue
for coin in ownedCoins:
print coin
item = cmcData[str(coin)]
v = item['value']
v = "%f" % (v)
print "+-----------------------------------+"
print "|" + outputLine('currency', item['currency'], "", "") + "|"
print "|" + outputLine('amount owned', v, "", "") + "|"
print "|" + outputLine('price in usd', item['price_usd'], "$", "") + "|"
print "|" + outputLine('price in btc', item['price_btc'], u'\u20BF', "") + "|"
print "|" + outputLine('% change 1h', item['d1h'], "", "%") + "|"
print "|" + outputLine('% change 24h', item['d24h'], "", "%") + "|"
print "|" + outputLine('% change 7d', item['d7d'], "", "%") + "|"
print "+-----------------------------------+"
print "\n"
我已经能够使用以下代码加载json:
当我尝试使用
json.load
而不是json.loads
时,我得到了一个错误(与您的不同),但它可能会给您一个关于可能出错的提示。你知道吗相关问题 更多 >
编程相关推荐