无法从 http 请求获取 json 响应

我正在尝试使用 requests 模块获取 JSON 响应。想知道是否有人知道可能导致这种情况的原因。


import requests


url = "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)


肥皂起泡泡
浏览 205回答 2
2回答

慕斯709654

从文档:如果 JSON 解码失败,r.json() 会引发异常。例如,如果响应得到 204(无内容),或者如果响应包含无效的 JSON,则尝试 r.json() 会引发 ValueError: No JSON object could be decoded。您需要一个url可能返回的 a json:import requestsurl = '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'}

喵喔喔

您返回的页面不是 json,而是它的 html
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python