1

I'm trying to pull data from a website. I'm using Python request:

users = requests.get('website name here', headers=headers).json()

I'm getting this error:

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

I know that the data i'm pulling is JSON, which is one of the issues that other people were getting with this error message. My guess is, because there's a username and password required to access the site, it's giving me this error. Could that be it? If so, how can i fix that? I don't want to include my login information in my Python program.

Edit: I now know that authorization was not the issue because i created an authorization token and i'm still getting the same issue.

Jason000
  • 179
  • 2
  • 5
  • 14
  • 4
    _I know that the data i'm pulling is JSON_ I'm confused what you mean by this. Do you mean that it **actually is** JSON, or do you mean that it **should be** JSON, assuming everything worked as expected? – John Gordon Sep 25 '19 at 19:06
  • 2
    Check the value of `.text` instead of `.json()`. It almost certainly isn’t valid json. If the site requires login, then you have to pass the username and password to your program somehow (unless the site offers an alternative method of authentication e.g. oauth). – Alasdair Sep 25 '19 at 19:07

1 Answers1

2

request.get() returns response (Doc) object and response.content will have actual response.

I would suggest trying:

import json
import requests

headers = {...}
response = requests.get('website name here', headers=headers)

try:
    users = json.loads(response.content)
except Exception as ex:
    print("Error getting response")
  • May be the response is not a valid json then. Try fetching the URL from browser/postman and validate the response on https://jsonformatter.curiousconcept.com. It should help you understand what is wrong. Happy learning! – Pranayjeet Thakare Sep 30 '19 at 21:44