错误:Updater.__init__() 收到意外的关键字参数“use_context”

Error: Updater.__init__() got an unexpected keyword argument 'use_context'

提问人:Davide Atzeni 提问时间:11/14/2023 最后编辑:khelwoodDavide Atzeni 更新时间:11/21/2023 访问量:60

问:

我正在尝试创建一个电报机器人,为我分析网页,返回第一个标签,其中包含一个强标签,只是它给了我标题中的错误。

这是我的机器人的完整代码:

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'
蟒蛇 python-asyncio python- 电报机器人

评论

0赞 furas 11/15/2023
似乎旧模块可以使用(请参阅版本 13.x),但最新版本不允许使用 .(见最新)。如果您使用某些教程,请首先检查它是何时创建的。也许这是非常古老的教程,它描述了旧的.Updater(use_content=True)use_contenttelegram
0赞 furas 11/15/2023
在官方源代码中,我找到了 Your-first-Bot,现在看来它的工作方式完全不同。它不使用python-telegram-bot/Updater

答: 暂无答案