繁体   English   中英

json.decoder.JSONDecodeError:期望值:第 1 行第 1 列(字符 0)python

[英]json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) python

我收到以下错误消息:

...文件“c:\\users\\dockerhost\\appdata\\local\\programs\\python\\python37\\Lib\\json\\decoder.py”,第 355 行,raw_decode 引发 JSONDecodeError("Expecting value", s, err.value)从无 json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

我正在尝试使用请求来获取响应标头。 我在尝试解决 jsondecodeerror 方面做了很多研究。 但是,我没有找到解决方案。

import requests
request.get('https://www.google.com/').json()

Error message.

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\Host\.virtualenvs\projects08-8iyGSYl4\lib\site-packages\requests\models.py", line 897, in json
    return complexjson.loads(self.text, **kwargs)
  File "c:\users\host\appdata\local\programs\python\python37\Lib\json\__init__.py", line 348, in loads
    return _default_decoder.decode(s)
  File "c:\users\host\appdata\local\programs\python\python37\Lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "c:\users\host\appdata\local\programs\python\python37\Lib\json\decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

JSON 解码器在第一行的第一个字符处期望一个值,这意味着它没有找到要解析的内容。 也就是说,您的响应对象的内容是空的。

您应该检查服务器响应的内容:

print(request.get(your_url).content)

以确保它不为空并且实际上是有效的 JSON。

如果它确实为空,则通常意味着您没有向服务器发送它期望的内容,并且您应该修复随请求发送的标头/cookies/身份验证/API 密钥/参数。

从文档中,

ResponseObj.json()

返回结果的 JSON 对象(如果结果是以 JSON 格式编写的,否则会引发错误)

您收到错误,因为结果不是 JSON 格式。

如果结果是有效的 JSON,

>>> requests.get('https://jsonplaceholder.typicode.com/todos/1').json()
{'userId': 1, 'id': 1, 'title': 'delectus aut autem', 'completed': False}

该函数确实返回一个 JSON。

使用request.get('https://www.google.com/').json()您盲目地将 HTTP 响应内容转换为 JSON。 响应可能是也可能不是 JSON,具体取决于 URL。 在这里,谷歌页面是 HTML 而不是 JSON。

文档中可以看出,如果内容不是 JSON,则json()调用将引发错误。

但是,您提到了响应标头。 响应标头可以通过request.get('https://www.google.com/').headers ,它是标头的字典表示。 如果您希望它是 json,只需像这样加载它

import json
import requests
header_json = json.dumps(dict(requests.get('https://www.google.com/').headers))
pprint(header_json)

这会给你这样的结果

{
    "Alt-Svc": "quic=\":443\"; ma=2592000; v=\"46,43,39\"",
    "Cache-Control": "private, max-age=0",
    "Content-Encoding": "gzip",
    "Content-Type": "text/html; charset=ISO-8859-1",
    "Date": "Wed, 28 Aug 2019 06:20:28 GMT",
    "Expires": "-1",
    "P3P": "CP=\"This is not a P3P policy! See g.co/p3phelp for more info.\"",
    "Server": "gws",
    "Set-Cookie": "######################removed purposefully##########################",
    "Transfer-Encoding": "chunked",
    "X-Frame-Options": "SAMEORIGIN",
    "X-XSS-Protection": "0"
}

这是您想要实现的目标吗?

为避免此错误,首先检查您是否收到不同于 200 的status_code 。可能服务器正在返回<Response [204]>

my_response = requests.get(MY_URL)
if my_response.status_code == 200:
    my_response.json()

else:
   ...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM