I am using the Yahoo finance library and the requests library in python. When I try to call historic data for ranges of months or years, like "1mo" or "1y" I get the error 422.
I tried to call another stock and different month periods like 3 months, but it is the same result. This is the code I use, which gives me the error code 422:
import requests
def fetch_yahoo_finance_data(symbol, interval='1m', range_='1mo'):
url
=f"https://query1.finance.yahoo.com/v8/finance/chart/{symbol}?
interval={interval}&range={range_}"
response = requests.get(url, headers=headers)
if response.status_code == 200:
data = response.json()
return data
else:
print(f"Failed to fetch data. Status
Code{response.status_code}")
return None
data = fetch_yahoo_finance_data('BMW.DE', "1m", "1mo")
i use the yahoo finance module
yfinanceto achieve the same (as opposed to using the URL method attmepted in the question which had an undefinedheadersvariable).this is the code that i use:
And the result look like this: