我目前正在编码一个不和谐的机器人,并且我有一个命令,该命令使用MyAnimeList API发送随机动漫的URL。这是我的代码:
@client.command()
async def anime(ctx):
await ctx.send("Récupération d'un anime...")
anime = 0
while anime == 0:
async with ctx.typing():
try:
ref = randrange(1, 40500)
anime = Anime(ref)
await ctx.send(anime)
except ValueError as err:
if str(err) == 'No such id on MyAnimeList':
pass
else:
pass
我正在使用while循环重试api是否返回404错误(因为并非在myanimelist.net上使用了每个动漫ID) 我得到的错误是:
Ignoring exception in command anime:
Traceback (most recent call last):
File "/home/container/.local/lib/python3.6/site-packages/discord/ext/commands/core.py", line 83, in wrapped
ret = await coro(*args, **kwargs)
File "bot.py", line 46, in anime
anime = Anime(ref)
File "/home/container/.local/lib/python3.6/site-packages/mal/anime.py", line 8, in __init__
super().__init__(mal_id, "anime", timeout)
File "/home/container/.local/lib/python3.6/site-packages/mal/mal.py", line 15, in __init__
title = self._page.find("meta", property="og:title")["content"]
TypeError: 'NoneType' object is not subscriptable
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/home/container/.local/lib/python3.6/site-packages/discord/ext/commands/bot.py", line 892, in invoke
await ctx.command.invoke(ctx)
File "/home/container/.local/lib/python3.6/site-packages/discord/ext/commands/core.py", line 797, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "/home/container/.local/lib/python3.6/site-packages/discord/ext/commands/core.py", line 92, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: 'NoneType' object is not subscriptable
我认为问题出在randrange()函数上。当我在寻找解决方案时,每个遇到相同问题的人都有一个涉及列表的代码,但是我的代码中没有任何代码... 我正在使用Python 3.7.6。 问题可能来自mal-api库本身...
答案 0 :(得分:0)
您可能会遇到限速或其他干扰正在返回的网页的问题。您可以尝试围绕Anime
编写包装类,以查看_page
属性,以了解问题所在:
class MyAnime(Anime):
def __init__(self, ref):
try:
super().__init__(ref)
except:
print(self._page)
print(self._page.find("meta"))
print(self._page.find("meta", property="og:title"))