在我的discord bot python中从wikipedia获取解析器错误

2024-04-26 07:57:32 发布

您现在位置:Python中文网/ 问答频道 /正文

下面是我尝试使用的代码。这是我使用重写discord.py的discord bot

@client.command()
async def wiki(self, ctx, arg):
    await ctx.send(f'Searching for {arg}.')
    print(wikipedia.summary(arg))
    await ctx.send({arg})

但是,每次尝试运行时都会出现一个错误,即:

/home/pi/.local/lib/python3.7/site-packages/wikipedia/wikipedia.py:389: 
UserWarning: No parser was explicitly specified, so I'm using the best available HTML parser
 for this system ("lxml"

这通常不是问题,但是如果您在另一个系统上或在不同的虚拟环境中运行此代码,它可能会使用不同的解析器并表现出不同的行为。你知道吗

导致此警告的代码位于文件的第389行 /home/pi/.local/lib/python3.7/site-packages/wikipedia/wikipedia.py。你知道吗

要消除此警告,请将附加参数'features="lxml"'传递给BeautifulSoup构造函数。你知道吗

lis = BeautifulSoup(html).find_all('li')  

我试过编辑bs4

soup = BeautifulSoup(sys.stdin)

soup = BeautifulSoup(sys.stdin, 'html.parser')

但是我仍然没有让它工作,在discord.py页面上询问GitHub会更好吗?你知道吗

我不知道会有什么帮助。你知道吗

编辑:

我已经改变了我的代码,它似乎工作得很好,但是,我读到在不和谐.py不是一个好主意,因为它停止了你的机器人,可能会停止它,导致重新启动。有没有可能不经请求就这样做?你知道吗

@client.command()
async def wiki(self, ctx, *, arg):
    input = arg
    print(wikipedia.page(input).url)
    await ctx.send(f'<{wikipedia.page(input).url}>')
    await ctx.send(wikipedia.summary(input, chars=500))

以上是我的新代码,它的工作完全符合预期。我只是想确定我不会弄坏我的机器人。你知道吗


Tags: 代码pyclientsendparserinputasyncdef
1条回答
网友
1楼 · 发布于 2024-04-26 07:57:32

这不是我们的问题不和谐.py而是an issue with the wikipedia library
然而,it should already be fixed在维基百科的最新开发版本中。你知道吗

你说得对,wikipedia库使用请求,请求是blocking。你知道吗

Is it possible to do this without a request?

您正在使用HTTP请求从Wikipedia获取信息,因此没有。
您需要考虑使用异步库来代替,或者使用异步库(如aiohttp)自己执行对wikipediaapi的请求。你知道吗

相关问题 更多 >