遇到问题
将requests请求相应结果json()化时,提示 "JSONDecodeError: Expecting value: line 1 column 1 (char 0)"
import json import requests resp = requests.get(url).json()
def raw_decode(self, s, idx=0): """Decode a JSON document from ``s`` (a ``str`` beginning with a JSON document) and return a 2-tuple of the Python representation and the index in ``s`` where the document ended. This can be used to decode a JSON document from a string that may have extraneous data at the end. """ try: obj, end = self.scan_once(s, idx) except StopIteration as err: > raise JSONDecodeError("Expecting value", s, err.value) from None E json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) ../../../../../.pyenv/versions/3.9.4/lib/python3.9/json/decoder.py:355: JSONDecodeError
解决方案:
这个报错的原因有很多,我的原因是域名或者uri写错了。导致请求响应了404, 这个响应消息体无法被json格式化,从而报错。更多原因可以参考:JSONDecodeError:期望值:第 1 行第 1 列(字符 0)
排查过程
直接执行上面的脚本,发现报出上述报警,可以看出是json格式出问题了,所以打算先去除json格式化这个步骤,先打印出response
import json import requests resp = requests.get(url).json()
去除json()
import json import requests resp = requests.get(url)
print("resp: %s" % resp)
运行结果如下
resp: <Response [404]>
发现响应了404, 说明可能是uri写错了, 排查uri之后,发现确实是写错了,改正uri之后,再次运行
运行结果如下
resp: <Response [200]>
响应正常了,再加上json()格式化语句,不报上述提示 "JSONDecodeError: Expecting value: line 1 column 1 (char 0)"问题了。说明是响应404导致响应体不能被json格式化,从而报错。