通过 YoBit API 提取数据时遇到值错误
Posted
技术标签:
【中文标题】通过 YoBit API 提取数据时遇到值错误【英文标题】:Running into a Value Error when extracting data through the YoBit API 【发布时间】:2018-05-06 23:23:24 【问题描述】:此代码的目的是从 Yobit(加密货币交易网站)上以 BTC 计价的每种货币收集数据。它确实收集了一些硬币的数据,但在大约 15 秒后出现错误。
以下是错误:
Traceback (most recent call last):
File "C:\Python34\yobit_update.py", line 168, in <module>
main()
File "C:\Python34\yobit_update.py", line 159, in main
datas = pairFinder(table)
File "C:\Python34\yobit_update.py", line 62, in pairFinder
tickerInfo(key)
File "C:\Python34\yobit_update.py", line 85, in tickerInfo
data = response.json()
File "C:\Python34\lib\site-packages\requests\models.py", line 894, in json
return complexjson.loads(self.text, **kwargs)
File "C:\Python34\lib\json\__init__.py", line 318, in loads
return _default_decoder.decode(s)
File "C:\Python34\lib\json\decoder.py", line 343, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Python34\lib\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)
下面是代码:
#pip install xlsxwriter
#pip install prettytable
#pip install requests
import xlsxwriter
from prettytable import PrettyTable
from requests import get
pair = ''
last = 0
highestBid = 0
lowestAsk = 0
volume24H = 0
smallPChange = 0
largePChange = 0
smalltolargePChange = 0
smallAsk = 0
largeAsk = 0
smallBuy = 0
largeBuy = 0
myBuy = 0
myPChange= 0
myAsk = 0
def fileSaving(datas):
# Creates the xlsx file
workbook = xlsxwriter.Workbook('cryptoInfo.xlsx')
worksheet = workbook.add_worksheet()
# Creates the headers on file
headers = 'Pair':'A1', 'Last':'B1', 'Bid':'C1', 'Ask':'D1', 'Volume':'E1', 'My Buy':'F1', 'My Ask':'G1', 'My % Increase':'H1', 'Small Buy':'I1', 'Small Ask':'J1', \
'Small % Inc':'K1', 'Large Buy':'L1', 'Large Ask':'M1', 'Large % Inc':'N1', 'Small to Large % Inc':'O1'
bold = workbook.add_format('bold':True)
for header in headers.keys():
worksheet.write(headers[header], header, bold)
# Adds the data to the file
for row in range(1, len(datas) -1):
for col in range(0,15):
worksheet.write(row, col, datas[row][col])
workbook.close()
def pairFinder(table):
datas = []
# Requests data from Yobit's API
response = get('https://yobit.net/api/3/info')
data = response.json()
# Iterates through the data for the pair names
print('Gathering Data For All: ' + str(len(data['pairs'].keys())) + ' Pairs.'\
+ ' Please Wait A Moment..')
i = 0
for key in data['pairs'].keys():
# Comment print below if you dont want screen spamming with this info
base = key.split('_')
if base[1] == 'btc':
print('Gathering Data Of: ' + key)
# Gets pair name and sends it through to get searched.
# Then stores it in variable and creates a table from the data
tickerInfo(key)
depthInfo(key)
# Adds a row to the screen table
data = [key, last, highestBid, lowestAsk, volume24H, myBuy, myAsk, myPChange, smallBuy, smallAsk, \
smallPChange, largeBuy, largeAsk, largePChange, smalltolargePChange]
datas.append(data)
# Expands the standard form
for value in range(1, len(data) -1):
if 'e' in str(data[value]):
data[value] = '0:.8f'.format(data[value])
table.add_row(data)
return datas
def tickerInfo(pair):
# Creates local variables
global volume24H, last, highestBid, lowestAsk
# Requests data from Yobit's API
response = get('https://yobit.net/api/3/ticker/' + pair)
data = response.json()
# Assigns wanted data to its variables
volume24H = data[pair]['vol']
last = data[pair]['last']
highestBid = data[pair]['buy']
lowestAsk = data[pair]['sell']
def depthInfo(pair):
global myBuy, myAsk, myPChange, smallAsk, largeAsk, smallPChange, largePChange, smalltolargePChange, smallBuy, largeBuy, lowestAsk
# Requests data from Yobit's API
response = get('https://yobit.net/api/3/depth/' + pair)
data = response.json()
# Assigns wanted data to its variables
try:
myBuy = 0.005
smallBuy = 0.05
largeBuy = 0.15
# Sets the Ask price
y = 0
for x in range(0, len(data[pair]['asks']) -1):
y = y + (data[pair]['asks'][x][0] * data[pair]['asks'][x][1])
if y >= myBuy:
myAsk = data[pair]['asks'][x][0]
break
y = 0
for x in range(0, len(data[pair]['asks']) -1):
y = y + (data[pair]['asks'][x][0] * data[pair]['asks'][x][1])
if y >= smallBuy:
smallAsk = data[pair]['asks'][x][0]
break
y = 0
for x in range(0, len(data[pair]['asks']) -1):
y = y + (data[pair]['asks'][x][0] * data[pair]['asks'][x][1])
if y >= largeBuy:
largeAsk = data[pair]['asks'][x][0]
break
# Sets the percentage change from inital asking price, (((new - old)/old) * 100)
myPChange = ((myAsk - lowestAsk) / lowestAsk) * 100
smallPChange = ((smallAsk - lowestAsk) / lowestAsk) * 100
largePChange = ((largeAsk - lowestAsk) / lowestAsk) * 100
smalltolargePChange = ((largeAsk - smallAsk) / smallAsk) * 100
print(smalltolargePChange)
# Change random_pair! to a pair you want to alter/change
#if pair == 'random_pair!':
# Add the code you want,
# smallBuy = (data[pair]['bids'][0][0] * YOUR EDIT VALUE)
# smallBuy = VALUE
# largeBuy = (data[pair]['bids'][(len(data[pair]['bids']) - 1)][0] *\
# YOUR VALUE)
# largeBuy = VALUE
# If you want to change multiple values of different pairs just copy the
# if statement again and change random_pair to the other pair
#smallBuy = 0.12
#largeBuy = 0.22
except:
pass
def main():
# Creates table headers
table = PrettyTable(['Pairs', 'Last', 'Bid', 'Ask', 'Volume', 'My Buy',\
'My Ask', 'My % Inc', 'Small Buy',\
'Small Ask', 'Small % Inc', 'Large Buy', 'Large Ask',\
'Large % Inc', 'Small to Large % Inc'])
# Starts the data gathering
datas = pairFinder(table)
print(table)
ans = input("Would you like to save this file?")
if 'yes' == ans.lower() or 'y' == ans.lower():
fileSaving(datas)
if __name__ == '__main__':
main()
此代码过去可以运行,但最近才开始出现此错误
谢谢
【问题讨论】:
【参考方案1】:在禁止您的请求之后,Yobit 的呼叫限制为每分钟 100 次。查看浏览器中的链接,看看你是否被临时封禁
https://yobit.net/api/3/ticker/ltc_btc
【讨论】:
以上是关于通过 YoBit API 提取数据时遇到值错误的主要内容,如果未能解决你的问题,请参考以下文章
Python 中的 API 调用身份验证(工作 PHP 示例)