简体   繁体   中英

Random json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

I am new to Python coding, and I have recently learned about APIs and JSON. I wrote a small piece of test code to interact with a random number generator ( https://www.random.org/ ). It has been working great, but recently I have been running into a JSONDecodeError rather randomly and inconstantly. It happens sometimes, but not all the times. I can run the code, and get the error, change nothing, and then run the code 10 mins later and not get an error. I did double check to make sure the API is running through the browser. Here are the basics:

Written and run through:
PyCharm Community Edition 2016.3.1
Build #PC-163.9735.8, built on December 14, 2016
JRE: 1.8.0_112-release-408-b6 x86
JVM: OpenJDK Server VM by JetBrains sro

import requests
import json

print("Random Number Generator:")
print("========================")

# RANDOM NUMBER GEN
response01 = requests.get(
"https://www.random.org/integers/?num=1&min=1&max=999999999&col=1&base=10&format=plain&rnd=new")
data1 = response01.json()

print(type(data1))
print(data1)

This code just prints a random number from the API. Here's the random error I get:

Random Number Generator:
========================
Traceback (most recent call last):
  File "C:/REDACTED/REDACTED/REDACTED/REDACTED/API Practice 2.py", line 10, in <module>
    data1 = response01.json()
  File "C:\Program Files (x86)\Python35-32\lib\site-packages\requests\models.py", line 850, in json
    return complexjson.loads(self.text, **kwargs)
  File "C:\Program Files (x86)\Python35-32\lib\json\__init__.py", line 319, in loads
    return _default_decoder.decode(s)
  File "C:\Program Files (x86)\Python35-32\lib\json\decoder.py", line 339, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Program Files (x86)\Python35-32\lib\json\decoder.py", line 357, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Process finished with exit code 1

So the question I have is sort of a two part.
A) What could be the cause of this error?
B) Why is it that it occurs (seemingly) randomly?

Thanks for any information that anyone can provide. Again, I am a NOOB to Python, so please bare with my ignorance or lack or education with it.

NOTE: REDACTED is just a placeholder.

Sometimes you'll get HTTP errors, then there is no content to the response object, so you can't parse it.

import requests
import json

print("Random Number Generator:")
print("========================")

# RANDOM NUMBER GEN
response01 = requests.get("https://www.random.org/integers/?num=1&min=1&max=999999999&col=1&base=10&format=plain&rnd=new")
try :
    data1 = json.loads(response01)
    print('[i] Response : %s' % data1)
except JSONDecodeError as e :
    print('[!] Error while decoding response contents')
    print('[!] %s - %s' % (response01.status_code, response01.text))
    # here you could replay the request

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