[Python] Yahoo Finance V7 API now requiring cookies? (Python)

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

  1. Stack

    Stack Membro Participativo

    url = 'https://query2.finance.yahoo.com/v7/finance/quote?symbols=TSLA&fields=regularMarketPreviousClose&region=US&lang=en-US'
    headers = {
    'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36'
    }
    data = requests.get(url,headers=headers)
    prepost_data = data.json()


    It seems recently, Yahoo Finance changed their V7 API to require cookies for every request. Running the code above, I get the Invalid Crumb error

    {"finance":{"result":null,"error":{"code":"Unauthorized","description":"Invalid Crumb"}}}

    This issue seems to also be known in this Github repo: https://github.com/joshuaulrich/quantmod/issues/382

    They seem to have a patch that works: https://github.com/joshuaulrich/quantmod/pull/383/commits

    But the code is all written in R... Anyone know how to translate this to Python?

    Continue reading...

Compartilhe esta Página