url = 'https://query2.finance.yahoo.com/v7/finance/quote?symbols=TSLA&fields=regularMarketPreviousClose®ion=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?
This worked for me.
set-cookie
from response headers which is then used in the subsequent calls[crumb-value]
in the following URL and make a HTTP GET call with cookie https://query2.finance.yahoo.com/v7/finance/quote?symbols=TSLA&crumb=[crumb-value]