Python - 尝试使用意外的 mimetype : 解码 JSON

标签 python json python-3.x python-asyncio aiohttp

我最近从 requests 切换到 aiohttp,因为我无法在 asyncio 循环中使用它。

交换进行得很顺利,除了一件事之外,一切都很顺利。我的控制台充满了

Attempt to decode JSON with unexpected mimetype:

Attempt to decode JSON with unexpected mimetype: txt/html; charset=utf-8

我的代码有一个站点列表,它也可以从中获取 JSON,每个站点都是不同的,但我的循环对于每个站点来说基本上是相同的,我在这里简化了它:

PoolName = "http://website.com"
endpoint = "/api/stats"
headers = "headers = {'content-type': 'text/html'}" #Ive tried "application/json" and no headers
async with aiohttp.get(url=PoolName+endpoint, headers=headers) as hashrate:
                hashrate = await hashrate.json()
endVariable = hashrate['GLRC']['HASH']

它工作完美,连接到站点抓取 json 并正确设置 endVariable。但由于某种原因

Attempt to decode JSON with unexpected mimetype:

每次循环时都会打印。这很烦人,因为它会将统计信息打印到控制台,并且每次获取站点 json 时都会迷失在错误中

有办法修复或隐藏此错误吗?

最佳答案

将预期的内容类型传递给 json() 方法:

data = await resp.json(content_type='text/html')

或完全禁用检查:

data = await resp.json(content_type=None)

关于Python - 尝试使用意外的 mimetype : 解码 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48840378/

相关文章:

python - 列表理解清理的更清洁方法

Python 索引超出范围解决方法

python - 为什么很多ORM在查询中不使用纯Python比较?

python - 将附加数据添加到 django Rest Framework View 集响应

python - 在 Python 中使用 multiprocessing.Array 时出现 OSError (Errno 9)

Python-如何确定我的系统可以处理的最大/最小整数/长整数/ float /复数

python - 如何在python中使用urllib2捕获重定向的url

python - 如何将 lazy_gettext 与 Pylons 和 setup.py 一起使用

json - 在 Wildfly 上激活 JaxbAnnotationModule

python - 在中等大小的 JSON 文件上使用线程池进行同步读取比异步读取更快