【问题标题】:How to fix "AttributeError: 'Client' object has no attribute 'send_message'" with Beautiful Soup 4 scraping HTML url如何使用 Beautiful Soup 4 抓取 HTML url 修复“AttributeError: 'Client' object has no attribute 'send_message'”
【发布时间】:2019-11-23 05:52:38
【问题描述】:

我正在尝试从 URL 中抓取 HTML 并让我的不和谐机器人显示结果。朋友提供了代码并进行了测试。但是,我相信他使用的是旧版本的 discord.py。他目前不可用。我已经搜索了所需的适当更改,但没有找到针对我的问题的特定更改。

我尝试将 client.send_message 更改为 channel.send - 我收到另一个错误:AttributeError: 'NoneType' object has no attribute 'send'

import discord
import requests
from bs4 import BeautifulSoup

client = discord.Client()

@client.event
async def on_ready():
    print('We have logged in as {0.user}'.format(client))

channel = client.get_channel('CHANNEL ID')

@client.event
async def on_message(message):
    if message.author == client.user:
        return

    if message.content.startswith('!dfx2'):
        website_url = requests.get('http://novaworld.cc/dfx2lobby.php?lob=pub').text
        soup = BeautifulSoup(website_url, 'html.parser')
        table = soup.find('table')
        test = table.select_one('tr:contains("!GET SOME")')
        text = test.get_text()
        print(text)
        await channel.send(message.channel, content = text)

client.run('TOKEN')

我收到错误 AttributeError: 'NoneType' object has no attribute 'send' after changed client.send_message

【问题讨论】:

标签: python python-3.x discord discord.py


【解决方案1】:

您的 channel.send 中不需要 message.channel。我会用这个:

channel = message.channel
await channel.send(content = text)

让我知道这是如何工作的 :)

【讨论】:

    【解决方案2】:

    好吧,我从未使用过 bs4,但我会尽力提供帮助。要获取频道,您必须使用

    channel = discord.utils.get(message.guild.channels, id=message.channel.id)
    

    然后如果您使用更新版本的 discord.py(重写)只需使用发送此消息

    await channel.send(text)
    

    【讨论】:

    • get 调用正在寻找与其所属公会具有相同 id 的频道,由于 id 是唯一的,因此不可能发生这种情况。
    • 你说得对...我把它编辑成message.channel.id
    • 为什么不直接使用message.channelawait message.channel.send(text)
    猜你喜欢
    • 2019-05-28
    • 1970-01-01
    • 2022-11-11
    • 1970-01-01
    • 2022-12-03
    • 2015-07-06
    • 2022-12-01
    • 2022-01-17
    • 2017-05-10
    相关资源
    最近更新 更多