【发布时间】:2019-04-01 21:06:43
【问题描述】:
当我尝试使用 python 请求时,我得到以下信息:当我尝试确保请求是 json 格式时。使用requests.get(url)时的请求是<Response [200]>。
但是,当使用request.get(url).json() 时,我得到以下信息:
File "/usr/local/lib/python3.6/json/__init__.py", line 354, in loads
return _default_decoder.decode(s)
File "/usr/local/lib/python3.6/json/decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/local/lib/python3.6/json/decoder.py", line 357, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
打印requests.get(url).headers 给出以下信息:
{'Server': 'nginx/1.10.3 (Ubuntu)', 'Date': 'Mon, 01 Apr 2019 21:03:28 GMT', 'Content-Type': 'text/html', 'Last-Modified': 'Mon, 25 Mar 2019 21:25:01 GMT', 'Transfer-Encoding': 'chunked', 'Connection': 'keep-alive', 'ETag': 'W/"5c99472d-806"', 'Strict-Transport-Security': 'max-age=63072000; includeSubdomains', 'X-Frame-Options': 'DENY', 'X-Content-Type-Options': 'nosniff', 'Content-Encoding': 'gzip'}
我试过剪掉某些字符,例如 、\、\0,但这并不影响结果。
【问题讨论】:
-
您的响应中的服务器标头显示内容类型是
text/html,而不是application/json。您可以使用.content属性查看正文包含的内容。 -
是的,如何才能将其更改为 application/json?
-
您的 JSON 似乎在使用单引号,请尝试将内容类型更改为
application/json,将页眉设置为:Content-Type,将您的页面提供给application/json。 -
@PrestonHager 这不是 json,这是代表响应标头的请求的字典。
-
如何更改内容类型?
标签: python json flask python-requests