我似乎无法使用JSON从网站API获取最后记录的价格。我试图找到错误,但对我来说似乎还可以。代码在python中 这是我必须获取的网址:https://api.independentreserve.com/Public/GetMarketSummary?primaryCurrencyCode=xbt&secondaryCurrencyCode=aud
Python 3.7
import requests
URL = "https://api.independentreserve.com/Public/GetMarketSummary?"
CurrencyCode = "xbt"
SecondaryCode = "aud"
PARAMS = {'primaryCurrencyCode': CurrencyCode, '&secondaryCurrencyCode': SecondaryCode}
r = requests.get(url=URL, params=PARAMS)
data = r.json()
lastprice = data['LastPrice']
print("Last Price:%s" % lastprice)
答案 0 :(得分:0)
这是固定代码
import requests
URL = "https://api.independentreserve.com/Public/GetMarketSummary?"
CurrencyCode = "xbt"
SecondaryCode = "aud"
PARAMS = {'primaryCurrencyCode': CurrencyCode, 'SecondaryCurrencyCode': SecondaryCode}
r = requests.get(url=URL, params=PARAMS)
data = r.json()
lastprice = data['LastPrice']
print("Last Price:%s" % lastprice)
问题出在PARAMS
字典中。您需要将"&secondaryCurrencyCode"
更改为"SecondaryCurrencyCode"
。
如果您已打印data
字典,则会看到以下内容:
{'Message': 'Secondary Currency Code is required'}
答案 1 :(得分:0)
删除&
中的"&secondaryCurrencyCode"
将解决此问题。
下面的固定代码:
import requests
URL = "https://api.independentreserve.com/Public/GetMarketSummary?"
CurrencyCode = "xbt"
SecondaryCode = "aud"
PARAMS = {'primaryCurrencyCode': CurrencyCode, 'secondaryCurrencyCode': SecondaryCode}
r = requests.get(url=URL, params=PARAMS)
data = r.json()
lastprice = data['LastPrice']
print("Last Price:%s" % lastprice)
答案 2 :(得分:0)
API期望secondaryCurrencyCode
不能&secondaryCurrencyCode
。
使用参数时不需要签名。