为了账号安全,请及时绑定邮箱和手机立即绑定

无法从 http 请求获取 json 响应

无法从 http 请求获取 json 响应

肥皂起泡泡 2022-01-11 16:05:36
我正在尝试使用 requests 模块获取 JSON 响应。想知道是否有人知道可能导致这种情况的原因。import requestsurl = "https://www.google.com/"data = requests.get(url)data.json()错误:提高 JSONDecodeError("Expecting value", s, err.value) from None json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
查看完整描述

2 回答

?
慕斯709654

TA贡献1840条经验 获得超5个赞

从文档:


如果 JSON 解码失败,r.json() 会引发异常。例如,如果响应得到 204(无内容),或者如果响应包含无效的 JSON,则尝试 r.json() 会引发 ValueError: No JSON object could be decoded。


您需要一个url可能返回的 a json:


import requests


url = 'https://github.com/timeline.json'    

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

print(data)

输出:


{'message': 'Hello there, wayfaring stranger. If you’re reading this then you probably didn’t see our blog post a couple of years back announcing that this API would go away: http://git.io/17AROg Fear not, you should be able to get what you need from the shiny new Events API instead.', 'documentation_url': 'https://developer.github.com/v3/activity/events/#list-public-events'}



查看完整回答
反对 回复 2022-01-11
?
喵喔喔

TA贡献1735条经验 获得超5个赞

您返回的页面不是 json,而是它的 html


查看完整回答
反对 回复 2022-01-11
  • 2 回答
  • 0 关注
  • 204 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号