I am trying to extract btcusdt price from binance to a google spreadsheet on my gdrive in vain.
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.
Note that 'k' is a dictionary.
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.
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.