Выводит ошибку отсутствия на сайте json, как обойти эту ошибку, чтоб выводить где есть json, а где не присутствует json?
Traceback (most recent call last):
File "C:\Users\bisne\AppData\Local\Programs\Python\Python312\Lib\site-packages\requests\models.py", line 971, in json
return complexjson.loads(self.text, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\bisne\AppData\Local\Programs\Python\Python312\Lib\json\__init__.py", line 346, in loads
return _default_decoder.decode(s)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\bisne\AppData\Local\Programs\Python\Python312\Lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\bisne\AppData\Local\Programs\Python\Python312\Lib\json\decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "d:\Games\новое.py", line 6, in <module>
print(response.json())
^^^^^^^^^^^^^^^
File "C:\Users\bisne\AppData\Local\Programs\Python\Python312\Lib\site-packages\requests\models.py", line 975, in json
raise RequestsJSONDecodeError(e.msg, e.doc, e.pos)
requests.exceptions.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
Ответы (1 шт):
Автор решения: Leonid
→ Ссылка
Проблема в том, что веб-сайт не вернул никаких json данных, вы можете это обработать блоком try/except:
try:
print(response.json())
except requests.exceptions.JSONDecodeError:
print('JSON ответ пуст')