我最近从 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/