我正在为价格创建电报机器人,但是我成功完成了,但是问题是价格没有更新,每次按启动时,我都会在API wep页面上获得相同的价格
import requests
import json
import telebot
import locale
import time
from datetime import datetime
locale.setlocale( locale.LC_ALL, '' )
while True :
try:
# base URLs
tickerURL = "https://api.coinmarketcap.com/v1/ticker/"
#
choice = "bitcoin"
tickerURL += '/'+choice+'/'
request = requests.get(tickerURL)
data = request.json()
ticker = data[0]['symbol']
price = data[0]['price_usd']
price_btc = data[0]['price_btc']
rank = data[0]['rank']
market_cap = data[0]['market_cap_usd']
change_24 = data[0]['24h_volume_usd']
price_a = ticker + ":\t\t$ " + price
price_b = ticker + ":\t\tB " + price_btc
bot_token = '<telegram-token>'
bot = telebot.TeleBot(bot_token)
@bot.message_handler(commands=['price', 'help'])
def send_welcome(message):
cont = price_a
cont += '\n'+ price_b
cont += '\nRank: '+ rank
cont += '\nChange 24hr: '+ "{:,}".format(float(change_24))
cont += '\nMarket_cap: ' + "{:,}".format(float(market_cap))
bot.reply_to(message,cont)
print('hey..')
bot.polling()
except Exception:
time.sleep(10)
所以我想添加一些代码以经常更新价格
答案 0 :(得分:0)
尝试以下代码段:
sh