Python Json не видит массив
Делаю чекер для гифтов Discord Проверяю видит ли python, Но выскакивает ошибка Код:
from urllib.request import urlopen
import json
url = "https://discordapp.com/api/v6/entitlements/gift-codes/1?with_application=false&with_subscription_plan=true"
with urlopen(url) as response:
source = response.read()
data = json.loads(source)
print(data["message"])
s = input()
Ошибка:
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 403: Forbidden
Ответы (1 шт):
Автор решения: Daniil Loban
→ Ссылка
Для корректной работы urlopen требует передачи в заголовке запроса (headers) аттрибута 'User-Agent' (например 'Mozilla/5.0 (Windows NT 6.1; Win64; x64)') но так как сервер в данном запросе возвращает код 404 это вызывает исключение HTTPError.
Поэтому лучше воспользоваться requests который позволяет получить тело ответа:
import requests
import json
url = "https://discordapp.com/api/v6/entitlements/gift-codes/1?with_application=false&with_subscription_plan=true"
response = requests.request("GET", url)
data = json.loads(response.text)
print(data["message"]) # Unknown Gift Code