Steve
Steve

Reputation: 21

Cannot get json response from http request

I am trying to get a JSON response using the requests module. Was wondering if anyone knows what could be causing this.

import requests

url = "https://www.google.com/"

data = requests.get(url)

data.json()

Error:

raise JSONDecodeError("Expecting value", s, err.value) from None json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Upvotes: 2

Views: 2603

Answers (2)

DirtyBit
DirtyBit

Reputation: 16782

From the docs:

In case the JSON decoding fails, r.json() raises an exception. For example, if the response gets a 204 (No Content), or if the response contains invalid JSON, attempting r.json() raises ValueError: No JSON object could be decoded.

You need to have a url that could possibly return a json:

import requests

url = 'https://github.com/timeline.json'    
data = requests.get(url).json()    
print(data)

OUTPUT:

{'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'}

Upvotes: 6

Bendik Knapstad
Bendik Knapstad

Reputation: 1458

The page you are returning is not json, its html

Upvotes: 1

Related Questions