简体   繁体   English

如何使用从 Binance API 收到的数据更新谷歌电子表格

[英]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.

相关问题 如何从谷歌电子表格中提取数据到笔记本 - How to extract data from a google spreadsheet to notebook 如何使用 Google Sheets API 和 Python 更新电子表格属性? - How to update Spreadsheet Properties with Google Sheets API and Python? 如何使用其API迭代解析Google电子表格中每个“子工作表”中的数据? - How do I iteratively parse data from each “sub-sheet” in a Google spreadsheet using their API? 如何使用 Python 中的 Google Sheets API 在电子表格的特定选项卡中写入数据? - How to write data in a specific tab of a spreadsheet with the Google Sheets API in Python? 如何操作来自币安 stream 的数据 - How to manipulate data from binance stream Python / Binance API - 如何从 API 抓取 Binance Leaderboard 期货 position? - Python / Binance API - How to scrape Binance Leaderboard futures position from API? 如何使用 dataframe 中的数据更新现有的 excel 电子表格 - How to update an existing excel spreadsheet with data from dataframe websocket-client python 没有来自 binance 的数据 api - websocket-client python no data from the binance api 从Binance API中提取数据并将其转换为PANDAS数据框 - Pulling data from Binance API and turning it into a PANDAS Dataframe 你能在没有认证的情况下从 Binance API 获取任何数据吗? - Can you get any data from the Binance API without authentification?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM