Answer the question
In order to leave comments, you need to log in
Why does the bot output function at 0x?
When you run the code and send a command to the bot, the bot issues
import telebot, requests, bs4
from bs4 import BeautifulSoup
bot = telebot.TeleBot("TOKEN")
def pars():
page_link = 'https://random.cat/'
response = requests.get(page_link)
html = response.content
soup = BeautifulSoup(html, 'html.parser')
obj = soup.find('img', attrs={'id': 'cat'})
return print(obj.attrs['src'])
@bot.message_handler(commands=['rcat'])
def randomcat(message):
bot.send_message(message.chat.id, pars)
bot.polling(none_stop=True)
Answer the question
In order to leave comments, you need to log in
Because you are passing an object to the function, not calling it.
Also, you need to remove the print function from the return - it just prints the data to the screen.
Seems like the correct code is:
import telebot, requests, bs4
from bs4 import BeautifulSoup
bot = telebot.TeleBot("TOKEN")
def pars():
page_link = 'https://random.cat/'
response = requests.get(page_link)
html = response.content
soup = BeautifulSoup(html, 'html.parser')
obj = soup.find('img', attrs={'id': 'cat'})
return obj.attrs['src']
@bot.message_handler(commands=['rcat'])
def randomcat(message):
bot.send_message(message.chat.id, pars())
bot.polling(none_stop=True)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question