简体   繁体   中英

Python request returning a JSON Decoder Error

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM