json - JSONDecodeError:期望值:json()的第1行第1列(char 0)在 Python3 上
问题描述
我正在尝试将在 Python2 上运行良好的代码移植到 Python3。我试图修复一些错误,但现在它与 json() 在 . 代码如下所示
resp = requests.get("https://status.github.com/api/status.json")
print(resp.ok)
print(resp.status_code)
print(resp.headers['content-type'])
print(resp)
x = resp.json()
调试结果如下
> True 200 text/html; charset=utf-8 <Response [200]>
> --------------------------------------------------------------------------- JSONDecodeError Traceback (most recent call
> last) <ipython-input-40-98ddeb8de13d> in <module>()
> 4 print(resp.headers['content-type'])
> 5 print(resp)
> ----> 6 x = resp.json()
>
> D:\ProgramData\Anaconda\lib\site-packages\requests\models.py in
> json(self, **kwargs)
> 894 # used.
> 895 pass
> --> 896 return complexjson.loads(self.text, **kwargs)
> 897
> 898 @property
>
> D:\ProgramData\Anaconda\lib\json\__init__.py in loads(s, encoding,
> cls, object_hook, parse_float, parse_int, parse_constant,
> object_pairs_hook, **kw)
> 346 parse_int is None and parse_float is None and
> 347 parse_constant is None and object_pairs_hook is None and not kw):
> --> 348 return _default_decoder.decode(s)
> 349 if cls is None:
> 350 cls = JSONDecoder
>
> D:\ProgramData\Anaconda\lib\json\decoder.py in decode(self, s, _w)
> 335
> 336 """
> --> 337 obj, end = self.raw_decode(s, idx=_w(s, 0).end())
> 338 end = _w(s, end).end()
> 339 if end != len(s):
>
> D:\ProgramData\Anaconda\lib\json\decoder.py in raw_decode(self, s,
> idx)
> 353 obj, end = self.scan_once(s, idx)
> 354 except StopIteration as err:
> --> 355 raise JSONDecodeError("Expecting value", s, err.value) from None
> 356 return obj, end
>
JSONDecodeError:期望值:第 1 行第 1 列(字符 0)
解决方案
resp = requests.get("https://status.github.com/api/status.json")
print(resp.headers['content-type'])
文本/html;字符集=utf-8
用于resp.headers['content-type']
查找链接 web 是text/html
.
因此 resp.json() 不适用于这种格式。也许你想试试 Beautifulsoup。
推荐阅读
- excel - “读取”文件名中的汉字
- reactjs - How to refresh a nested Component view that shows the result for a search being done in React
- mongoose - 如何在值数组中找到所有具有属性值的元素 - Mongoose
- awk - 多个 gzip 文件输入和多个 if else 到 awk
- node.js - 将 1 个 Textarea 行分隔为数组以将它们存储在 MongoDB 中
- c# - 单击按钮后如何在数据网格中创建新行
- python - 带有 tz 偏移的日期时间的 strptime 示例
- python - Seaborn 线图 - 仅显示重复 CSV 数据的部分日期
- javascript - MarkLogic 错误实例未使用实例转换器模块定义
- javascript - 使用 Javascript 单击按钮获取随机图像