zjtzang
Junior Member
hiện e đang định code 1 con bot telegram để cập nhật tin tức tự động. E có copy code trên mạng sẵn r paste vào nhưng còn vài chỗ lấn cấn. Bác nào rảnh hướng dẫn e với ạ. E k biết gì về code hết .
đoạn code như dưới nhưng em muốn giới hạn số kết quả hiển thị lại về 7 tittle. Em có thêm hàm while vào nhưng nó k chạy.
from telegram import Update
from telegram.ext import Updater, CommandHandler, CallbackContext
import requests
from bs4 import BeautifulSoup
def get_news():
list_news = []
r = requests.get("https://vnexpress.net/")
soup = BeautifulSoup(r.text, 'html.parser')
mydivs = soup.find_all("h3", {"class": "title-news"})
for new in mydivs:
newdict = {}
newdict["link"] = new.a.get("href")
newdict["title"] = new.a.get("title")
list_news.append(newdict)
return list_news
def hello(update: Update, context: CallbackContext) -> None:
update.message.reply_text(f'xin chao {update.effective_user.first_name}')
def news(update: Update, context: CallbackContext) -> None:
data = get_news()
str1 = ""
for item in data:
str1 += item["link"] + "\n"
update.message.reply_text(f'{str1}')
updater = Updater('2138262272:AAEl33TTHD50qQcUE7aixaknDfOnyEpfLAU')
updater.dispatcher.add_handler(CommandHandler('hello', hello))
updater.dispatcher.add_handler(CommandHandler('news', news))
updater.start_polling()
updater.idle()
đoạn code như dưới nhưng em muốn giới hạn số kết quả hiển thị lại về 7 tittle. Em có thêm hàm while vào nhưng nó k chạy.
from telegram import Update
from telegram.ext import Updater, CommandHandler, CallbackContext
import requests
from bs4 import BeautifulSoup
def get_news():
list_news = []
r = requests.get("https://vnexpress.net/")
soup = BeautifulSoup(r.text, 'html.parser')
mydivs = soup.find_all("h3", {"class": "title-news"})
for new in mydivs:
newdict = {}
newdict["link"] = new.a.get("href")
newdict["title"] = new.a.get("title")
list_news.append(newdict)
return list_news
def hello(update: Update, context: CallbackContext) -> None:
update.message.reply_text(f'xin chao {update.effective_user.first_name}')
def news(update: Update, context: CallbackContext) -> None:
data = get_news()
str1 = ""
for item in data:
str1 += item["link"] + "\n"
update.message.reply_text(f'{str1}')
updater = Updater('2138262272:AAEl33TTHD50qQcUE7aixaknDfOnyEpfLAU')
updater.dispatcher.add_handler(CommandHandler('hello', hello))
updater.dispatcher.add_handler(CommandHandler('news', news))
updater.start_polling()
updater.idle()