G
G
ghazar7an2020-04-17 11:42:24
Python
ghazar7an, 2020-04-17 11:42:24

telegram bot || Can you help with a bot, parser?

I made a bot in a telegram with sending news from a specific site.
When I turn on the parser itself without a bot, it displays all the information from the news feed to the console, and when I turn on the bot and write a command in tg to get information, it sends only one piece of news. You can fix my code and make it send 5 or 10 news feeds instead of just one.

from bs4 import BeautifulSoup
import requests
import telebot

bot = telebot.TeleBot('config.token')
url = 'http://mignews.com/mobile'
page = requests.get(url)
new_news = []
soup = BeautifulSoup(page.text, "html.parser")
news = soup.findAll('a', class_='lenta')
for i in range(len(news)):
    if news[i].find('span', class_='time2 time3') is not None:
        new_news.append(news[i].text)
for i in range(len(new_news)):

    @bot.message_handler(commands=['start'])
    def start_message(message):
        bot.send_message(message.chat.id, 'Привет, ты написал мне /start')


    @bot.message_handler(content_types=['text'])
    def send_text(message):
        if message.text == 'Новости':
            bot.send_message(message.chat.id, new_news[:5])
        elif message.text == 'News':
            bot.send_message(message.chat.id, new_news[:5])


    bot.polling()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
Timur Pokrovsky, 2020-04-17
@ghazar7an

It is necessary to send not new_news[:5], but
' '.join(new_news[:5])

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question