1 回答
TA贡献1744条经验 获得超4个赞
该requests.Response对象有一个json方法可以为您读取 json。请注意,对于async函数,您不应该进行阻塞requests.get。本回答重点关注json部分。
@bot.command(name="meme")
async def memcommand(ctx, type: str = None):
if type is None:
subreddit = reddit.subreddit("memes")
submission = subreddit.random()
posturl = f"https://www.reddit.com/r/memes/comments/{submission}/.json"
try:
# urllib.request.urlretrieve(posturl, f'reddit_jsons/{submission}.json')
r = requests.get(posturl)
except Exception:
await ctx.send(f"Seems like reddit doesn't like this many requests... Let's wait a bit....")
return
actualfile = r.json()
imgurl = actualfile[0]['data']['children'][0]['data']['url']
添加回答
举报