亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

讀取內存中的一個json文件discord.py

讀取內存中的一個json文件discord.py

POPMUISE 2023-05-23 16:13:54
我正在嘗試從網站獲取一個 jsonfile 到內存中,然后讀取其中的內容。但我不太清楚該怎么做......這就是我所做的@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)            inmemoryfile = io.BytesIO(r.content)        except Exception:            await ctx.send(f"Seems like reddit doesn't like this many requests... Let's wait a bit....")            return        with open(inmemoryfile) as f: # This throws the error            actualfile = json.load(f)        imgurl = actualfile[0]['data']['children'][0]['data']['url']錯誤是Ignoring exception in command meme:Traceback (most recent call last):  File "C:\Users\xlysa\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped    ret = await coro(*args, **kwargs)  File "D:/Ficheiros/Pastas/Tudo/Coding/Python/Projects/Server-Utils/bot.py", line 958, in memcommand    with open(inmemoryfile) as f:TypeError: expected str, bytes or os.PathLike object, not _io.BytesIOThe above exception was the direct cause of the following exception:Traceback (most recent call last):  File "C:\Users\xlysa\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\bot.py", line 903, in invoke    await ctx.command.invoke(ctx)  File "C:\Users\xlysa\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 855, in invoke    await injected(*ctx.args, **ctx.kwargs)  File "C:\Users\xlysa\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 94, in wrapped    raise CommandInvokeError(exc) from exc如果不能這樣,我如何打開文件并查看其內容?
查看完整描述

1 回答

?
慕無忌1623718

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']


查看完整回答
反對 回復 2023-05-23
  • 1 回答
  • 0 關注
  • 156 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號