首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >尝试使用意外的mimetype解码JSON : text/html

尝试使用意外的mimetype解码JSON : text/html
EN

Stack Overflow用户
提问于 2021-09-19 10:58:29
回答 2查看 799关注 0票数 0

我的discord机器人中的meme命令以前是有效的,但它突然停止工作了……以下是代码

代码语言:javascript
复制
@commands.command(name = 'meme')
@commands.cooldown(1, 2, BucketType.user)
async def meme(self,ctx):
    '''get a random meme from reddit!'''
    subreddits = ['dankmemes', 'memes','meme', 'wholesomememes', 'comedyheaven','pewdiepiesubmissions', 'KidsAreFuckingStupid','cursedcomments','HolUp','blursedimages','rareinsults']
    subreddit = random.choice(subreddits)
    async with aiohttp.ClientSession() as cs:
        async with cs.get(f'https://www.reddit.com/r/{subreddit}/new.json?sort=hot') as r:
            res = await r.json()
            post=res['data']['children'][random.randint(0, 25)]
            url = post['data']['url']
            title= post['data']['title']
            embed = discord.Embed(title = title,description=f"Meme for {ctx.author}")
            embed.set_image(url=url)
            embed.set_footer(text = f'Image from r/{subreddit}')
            await ctx.send(embed=embed)

它几天前就停止工作了..我已经尝试寻找一个解决方案很长一段时间了,但没有得到它的工作,我尝试了这个:https://stackoverflow.com/a/48841071/16390831

然而,我尝试的每个解决方案都给出了两个错误中的一个:aiohttp.client_exceptions.ContentTypeError: 0, message='Attempt to decode JSON with unexpected mimetype: text/html', url=URL('https://www.reddit.com/r/meme/new.json?sort=hot')

File "C:\Users\dhrav\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 167, in wrapped ret = await coro(*args, **kwargs) File "c:\Users\dhrav\Documents\Python Projects\SpaceBot\SpaceBot\commands.py", line 565, in meme res = json.loads(rs) File "C:\Users\dhrav\AppData\Local\Programs\Python\Python39\lib\json\__init__.py", line 346, in loads return _default_decoder.decode(s) File "C:\Users\dhrav\AppData\Local\Programs\Python\Python39\lib\json\decoder.py", line 337, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "C:\Users\dhrav\AppData\Local\Programs\Python\Python39\lib\json\decoder.py", line 355, in raw_decode raise JSONDecodeError("Expecting value", s, err.value) from None json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

怎么啦?是不是和Reddit API有关?我试着打印cs,它给出了一个关于服务器之类的奇怪的输出。

同样,它在几天前确实起作用了,但突然停止工作,似乎没有任何原因。任何帮助都将不胜感激。提前谢谢。

EN

回答 2

Stack Overflow用户

发布于 2021-09-19 11:21:56

接口返回一个Content-Type: text/html头部,您可以将content_type=None传入r.json以忽略该头部:

代码语言:javascript
复制
res = await r.json(content_type=None)
票数 0
EN

Stack Overflow用户

发布于 2021-09-19 11:24:52

看起来Reddit发送了错误的内容类型。您可以将预期的内容传递给json()方法

代码语言:javascript
复制
res = await r.json(content_type='text/html')
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69242604

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档