python - 'ListingGenerator' 对象不可使用 ASYNCPRAW 进行迭代
问题描述
我想从 subreddit 中获取模因。问题是当我尝试使用该方法获取模因时,subreddit('memes')
该方法返回一个不可迭代的“ListingGenerator”对象。
我想知道是否有任何方法可以将其转换为可迭代对象或任何其他方法来使用 ASYNCPRAW 从 reddit 获取 meme。
这是功能:
async def meme(self, ctx):
subreddit = await reddit.subreddit('memes')
print(type(subreddit))
all_subs = []
print(subreddit.hot(limit=50))
for submission in subreddit.hot(limit=50):
all_subs.append(submission)
random_sub = random.choice(all_subs)
name = random_sub.title
url = random_sub.url
embed = discord.Embed(title=name)
embed.set_image(url=url)
await ctx.send(embed=embed)
这是我得到的错误:
Traceback (most recent call last):
File "C:\Users\ansel\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "C:\Users\ansel\PycharmProjects\Transfer News\cogs\meme.py", line 48, in meme
for submission in subreddit.hot(limit=50):
TypeError: 'ListingGenerator' object is not iterable
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\ansel\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ext\commands\bot.py", line 902, in invoke
await ctx.command.invoke(ctx)
File "C:\Users\ansel\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ext\commands\core.py", line 864, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "C:\Users\ansel\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ext\commands\core.py", line 94, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: 'ListingGenerator' object is not iterable
解决方案
推荐阅读
- javascript - 微前端在反应
- python - 使用 2D 噪声数据进行平滑曲线拟合
- ios - Swift 后台模式错误 Swift (CoreBluetooth)
- bash - 如何避免在数组中存储没有间距的值?
- c# - 显示上传到 Azure BLOB 的进度
- typescript - 如何在 ts 中导入导出单个函数的“成熟”库?
- ros - ROS MoveIt 通用机器人
- r - How can I remove zero and negative values in data frame?
- python - 由于闪烁的文本,程序在几秒钟后崩溃
- c# - 条码不使用 Dymo SDK 和 c# .net 打印