[Python] Why is session.get() returning unicode instead of json?

Discussão em 'Python' iniciado por Stack, Junho 21, 2025.

  1. Stack

    Stack Membro Participativo

    import requests
    from bs4 import BeautifulSoup
    import json

    session = requests.Session()

    url = "https://curiosa.io/decks/cmbcn15x3003cl704euvw894m"

    response = session.get(url)

    # load url from DevTools
    url = 'https://curiosa.io/api/trpc/deck.getDecklistById,deck.getAvatarById,deck.getSideboardById,deck.getMaybeboardById?batch=1&input=%7B%220%22%3A%7B%22json%22%3A%7B%22id%22%3A%22cmbcn15x3003cl704euvw894m%22%7D%7D%2C%221%22%3A%7B%22json%22%3A%7B%22id%22%3A%22cmbcn15x3003cl704euvw894m%22%7D%7D%2C%222%22%3A%7B%22json%22%3A%7B%22id%22%3A%22cmbcn15x3003cl704euvw894m%22%7D%7D%2C%223%22%3A%7B%22json%22%3A%7B%22id%22%3A%22cmbcn15x3003cl704euvw894m%22%7D%7D%7D'

    headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:139.0) Gecko/20100101 Firefox/139.0",
    "Accept": "*/*",
    "Accept-Language": "en-US,en;q=0.5",
    "Accept-Encoding": "gzip, deflate, br, zstd",
    "Referer": "https://curiosa.io/decks/cmbcn15x3003cl704euvw894m",
    "content-type": "application/json",
    "x-build-id": "f7b49bf06ceefe470ea1bd27506c319176a942f8",
    "x-trpc-source": "nextjs-react",
    }

    response = session.get(url, headers=headers)
    print(response.text)
    data = response.json()


    response.text prints out a bunch of unicode, instead of json.

    The json can be found through the original web page by going to DevTools (Firefox) and clicking on Network, then going by the image below.

    [​IMG]

    I tried printing out response.text to see if it was decipherable as json and that's where it was revealed that unicode is being obtained, rather than json. I double checked my headers for the session. data = response.json() throws this error:

    Traceback (most recent call last):
    File "//./sorcery.py", line 27, in <module>
    data = response.json()
    File "/usr/local/lib/python3.13/site-packages/requests/models.py", line 980, in json
    raise RequestsJSONDecodeError(e.msg, e.doc, e.pos)
    requests.exceptions.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

    Continue reading...

Compartilhe esta Página