A
A
a1exapp1e2019-12-27 06:35:46
Python
a1exapp1e, 2019-12-27 06:35:46

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

2 answer(s)
E
Elvis, 2019-12-27
@a1exapp1e

In return, remove print, it is not needed there

K
kiriharu, 2019-12-27
@kiriharu

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 question

Ask a Question

731 491 924 answers to any question