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']
添加回答
舉報