如何用Python为Telegram机器人实现es.pdfdrive.com书籍搜索并获取URL
开发需求与现有进度
我是没有任何Python开发经验的编程初学者,目前正在结合Python、Telegram机器人框架和BeautifulSoup开发Telegram机器人,已经成功实现了对https://es.pdfdrive.com/的基础数据爬取。当前需要实现的功能是:支持将书名输入该网站的搜索引擎,获取每本匹配目标书籍的访问URL;此前我没有在母语渠道找到相关参考资料,也不清楚该怎么检索对应的实现方法。做这个小项目就是为了熟悉Python的实用开发能力,还望各位多多指教。
现有爬取模块代码
from bs4 import BeautifulSoup import requests import pandas as pd url = 'https://es.pdfdrive.com/' page = requests.get(url) soup = BeautifulSoup(page.content, 'html.parser') #secciones trending = soup.find_all('h2') categories = soup.find_all('p') trenx = list() categ=list() con =0 for i in trending: if con < 5: trenx.append(i.text) else: break con += 1 for i in categories: if con < 15: categ.append(i.text) else: break con += 1 def trend_today(update, context): update.message.reply_text('Mostrando los 5 más populares') for x in trenx: update.message.reply_text(x) def categories_inlist(update, context): update.message.reply_text('Mostrando listado de Categorias') for x in categ: update.message.reply_text(x)
Telegram机器人主程序代码
from telegram.ext import Updater, CommandHandler,ConversationHandler from scra import trend_today,categories_inlist def start(update, context): update.message.reply_text('Bienvenido a Wissen') def prueba_CommandHandler(update,context): update.message.reply_text('') if __name__== '__main__': Updater = Updater(token='tokenid',use_context=True) dp = Updater.dispatcher dp.add_handler(CommandHandler('start',start)) dp.add_handler(ConversationHandler( entry_points=[ CommandHandler('Trend',trend_today), CommandHandler('Categories',categories_inlist) ], states={}, fallbacks=[] )) #handler Updater.start_polling() Updater.idle()
内容的提问来源于stack exchange,提问作者Gabriel Medranda Dev
相关产品推荐
相关产品推荐

