问题描述
我正在尝试制作一个小型电报价格机器人,但遇到了一个问题。
我的函数循环得很好,并在 while 循环中完美地打印出它自己抓取的结果。但是,当我为机器人添加部分代码时,它不再循环并且只打印一次。因此,当我向机器人询问价格时,他只返回刮取的第一个值而不会更新。
我的猜测是 bot.polling() 元素以某种方式干扰了 while 循环,因为它也是某种循环(它侦听用户输入)
有什么解决方法吗?
这里是我的代码:
import os
import telebot
from requests_html import HTMLSession
#Creating the bot with the attached API
API_KEY = ''
bot = telebot.TeleBot(API_KEY)
#token price scraping
def getPrice(url):
"""Return the price of token"""
s = HTMLSession()
r = s.get(url)
r.html.render(sleep=1)
r.html.render(timeout=2000)
product = {
'price': r.html.xpath('//*[@id="app"]/div[2]/div/div[2]/div[2]',first=True).text,'marketcap': r.html.xpath('//*[@id="app"]/div[2]/div/div[2]/div[4]',}
return product['price'],product['marketcap']
token_price = (f"The price is: {getPrice('')[0]}")
token_marketcap = (f"The market cap is: {getPrice('')[1]}")
print (token_price)
print (token_marketcap)
#Price bot commands
@bot.message_handler(commands=['greet'])
def greet(message):
bot.reply_to(message,"Hey! Hows it going?")
@bot.message_handler(commands=['hello'])
def hello(message):
bot.send_message(message.chat.id,"Hello!")
@bot.message_handler(commands=['price'])
def price(message):
bot.send_message(message.chat.id,token_price)
@bot.message_handler(commands=['marketcap'])
def marketcap(message):
bot.send_message(message.chat.id,token_marketcap)
bot.polling()```
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)