我使用 Python 3 通过下面的代码从 Instagram API 获取响应
instagramJSON = xRequest(nextUrl)
print (instagramJSON)
instagramDict = json.load(instagramJSON)
xRequest 方法从 Instagram API URL 获取响应,当我打印对象时它看起来像一个合法的 JSON 对象但是当我执行 json.load 方法时在它上面我得到以下错误 AttributeError: 'bytes' object has no attribute 'read'
相同的代码适用于 Python 2,我试图在 Python3 的 Python 文档中搜索它,但没有成功。谁能帮我解决这个问题?
非常感谢!
请您参考如下方法:
如果 instagramJSON 是 str,那么您应该使用 json.loads 而不是 json.load。如果 instagramJSON 是一个字符串,那么它会在 Python 2 中引发相同的异常,因此可能会发生更多事情。
如果它是由 urllib.request 生成的响应,那么顶部的答案是 this question可能是相关的。
