import datetime
import requests
from bs4 import BeautifulSoup
from bs4 import BeautifulStoneSoup
import telebot
import time
bot = telebot.TeleBot('token')
@bot.message_handler(commands=['start'])
def startmess(message):
sendmessstart = f"Привет {message.from_user.first_name}!\n Введите команду rub чтобы узнать курс доллара в рубли"
bot.send_message(message.chat.id, sendmessstart)
@bot.message_handler(commands=['rub'])
def send_text(message):
DOLLAR_RUB = 'https://www.google.com/search?q=%D0%BA%D1%83%D1%80%D1%81+%D0%B4%D0%BE%D0%BB%D0%BB%D0%B0%D1%80%D0%B0+%D0%B2+%D1%80%D1%83%D0%B1%D0%BB%D1%8F%D1%85&oq=%D0%BA%D1%83%D1%80%D1%81&aqs=chrome.0.69i59l2j69i57j0l2j69i61l3.1190j1j7&sourceid=chrome&ie=UTF-8'
headers = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36'}
full_page = requests.get(DOLLAR_RUB, headers=headers)
soup = BeautifulSoup(full_page.content, 'html.parser')
convert=soup.findAll("span", {"class": "DFlfde", "class": "SwHCTb", "data-precision": 2})
rub = (convert[0].text)
chatid = message.chat.id
bot.send_message(chatid, rub)
bot.polling(none_stop=True)
Python IndexError:列表索引超出范围
我们需要检查返回列表的长度(不是空的):