且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

JSON对象必须是str,而不是Response

更新时间:2023-12-04 12:05:04

您正在尝试将响应对象提供给json.loads().您在那里没有字符串,而必须访问.contents.text属性:

You are trying to feed the response object to json.loads(). You don't get a string there, you'd have to access the .contents or .text attribute instead:

response = requests.get(url)
# Python 3
response = json.loads(response.text)
# Python 2
response = json.loads(response.content)

但是,这样做的工作量超出了您的需要; requests支持直接处理JSON,无需导入json模块:

However, that'd be doing more work than you need to; requests supports handling JSON directly, and there is no need to import the json module:

response = requests.get(url).json()

请参见 JSON响应内容requests快速入门文档的部分.

See the JSON Response Content section of the requests Quickstart documentation.

加载后,您可以在嵌套词典中的doc键上键入response:

Once loaded, you can get the doc key in the nested dictionary keyed on response:

for documents in response['response']['docs']: