[英]How to update a google spreadsheet with data received from a Binance API
I am trying to extract btcusdt price from binance to a google spreadsheet on my gdrive in vain.我试图将 btcusdt 价格从币安提取到我的 gdrive 上的谷歌电子表格,但徒劳无功。
I tried the following:我尝试了以下方法:
import websocket, json, numpy as np
cc = 'btcusdt'
interval = '1m'
socket = f'wss://stream.binance.com:9443/ws/{cc}@kline_{interval}'
from google.colab import auth
auth.authenticate_user()
import gspread
import pandas as pd
from oauth2client.client import GoogleCredentials
keyid = 'mykeyid'
gc = gspread.authorize(GoogleCredentials.get_application_default())
wb = gc.open_by_key('mykeyid')
wrksheet = wb.worksheet('btcusdt')
def on_message(ws, message):
json_message = json.loads(message)
cs = json_message['k']
wrksheet.update('A5', cs[])
ws = websocket.WebSocketApp(socket, on_message=on_message)
ws.run_forever()
I need to append each message (cs) to a new row.我需要将每条消息 (cs) 附加到新行。
Note that 'k' is a dictionary.请注意,'k' 是一本字典。
Thanking you in advance.提前谢谢你。
The idea is to capture the last row of the worksheet, then get the value of that row and pass it to the find method available to determine the cell information.这个想法是捕获工作表的最后一行,然后获取该行的值并将其传递给可用于确定单元格信息的 find 方法。
def on_message(ws, message):
json_message = json.loads(message)
cs = json_message['k']
# This will get the last row but consider other slicing options if you have headers
last_row = wrksheet.get_all_values()[::-1]
# Pass the value, I assume you only have 1 column to this from your code 'A5'
cell = wrksheet.find(last_row[0])
# Increment the cell row to make sure it will populate the last row + 1
wrksheet.update('A'+str(cell.row+1), cs)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.