如何通过API下载reddit文章的标题



这是我用于discord bot的meme命令,如何获取所选reddit帖子的标题?

@client.command()
async def meme(ctx):
meme_subreddits = ['https://www.reddit.com/r/dankmemes/new.json?sort=hot',
'https://www.reddit.com/r/memes/new.json?sort=hot',
'https://www.reddit.com/r/meme/new.json?sort=hot',
'https://www.reddit.com/r/me_irl/new.json?sort=hot']
async with aiohttp.ClientSession() as cs:
async with cs.get(random.choice(meme_subreddits)) as r:
res = await r.json()
embed = discord.Embed(title="Meme Title", color=0x202225)
embed.set_image(url=res['data']['children'] [random.randint(0, 25)]['data']['url'])
await ctx.send(embed=embed)

你已经得到了那个随机的reddit帖子的图片,你只需要获取该帖子的标题:

...
random_post = res["data"]["children"][random.randint(0, 25)]
image_url = random_post["data"]["url"]
title = random_post["data"]["title"]
...

最新更新