提问人:Davide Atzeni 提问时间:11/14/2023 最后编辑:khelwoodDavide Atzeni 更新时间:11/21/2023 访问量:60
错误:Updater.__init__() 收到意外的关键字参数“use_context”
Error: Updater.__init__() got an unexpected keyword argument 'use_context'
问:
我正在尝试创建一个电报机器人,为我分析网页,返回第一个标签,其中包含一个强标签,只是它给了我标题中的错误。
这是我的机器人的完整代码:
import requests
from bs4 import BeautifulSoup
from telegram import Update
from telegram.ext import CommandHandler, CallbackContext, Updater
TOKEN = 'MY_TOKEN'
CHAT_ID = '@NOMEBOT'
async def start(update: Update, context: CallbackContext) -> None:
await update.message.reply_text('Ciao! Invia /scrape per eseguire lo scraping della pagina.')
async def scrape(update: Update, context: CallbackContext) -> None:
url = 'MYWEBSITE'
try:
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
first_a_tag = soup.find('a', {'strong': True})
if first_a_tag:
result = first_a_tag.text
await update.message.reply_text(f'Il testo nel primo tag <a> con <strong> è: {result}', parse_mode='HTML')
else:
await update.message.reply_text('Nessun tag <a> con <strong> trovato.')
except Exception as e:
await update.message.reply_text(f'Si è verificato un errore: {str(e)}')
async def main():
updater = Updater(use_context=True)
bot = updater.bot
bot.set_token(TOKEN)
dp = updater.dispatcher
dp.add_handler(CommandHandler("start", start))
dp.add_handler(CommandHandler("scrape", scrape))
updater.start_polling()
await updater.idle()
if __name__ == '__main__':
import asyncio
asyncio.run(main())
错误:
Traceback (most recent call last):
File "C:\Users\D\Desktop\BotTelegram\onepiezo.py", line 45, in <module>
asyncio.run(main())
File "C:\Users\D\AppData\Local\Programs\Python\Python312\Lib\asyncio\runners.py", line 194, in run
return runner.run(main)
^^^^^^^^^^^^^^^^
File "C:\Users\D\AppData\Local\Programs\Python\Python312\Lib\asyncio\runners.py", line 118, in run
return self._loop.run_until_complete(task)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\D\AppData\Local\Programs\Python\Python312\Lib\asyncio\base_events.py", line 664, in run_until_complete
return future.result()
^^^^^^^^^^^^^^^
File "C:\Users\D\Desktop\BotTelegram\onepiezo.py", line 31, in main
updater = Updater(use_context=True)
^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: Updater.__init__() got an unexpected keyword argument 'use_context'
答: 暂无答案
评论
Updater(use_content=True)
use_content
telegram
python-telegram-bot/
Updater