Reputation: 121
One of the use cases I use Yahoo finance API for is to find out the earnings date for a given stock. This was working fine till about 7/2021 but started giving error 403 (forbidden).
After struggling a while, found that adding a {'User-agent': 'Mozilla/5.0'}
header would fix the issue. If you face a similar issue, you could try and see if it fixes for you too. Here is a sample screenshot:
>>> url="https://query2.finance.yahoo.com/v10/finance/quoteSummary/PYPL?modules=calendarEvents"
>>> r=requests.get(url)
>>> r
<Response [403]>
>>> r=requests.get(url, headers={'User-agent': 'Mozilla/5.0'})
>>> r
<Response [200]>
>>> r.json()
{'quoteSummary': {'result': [{'calendarEvents': {'maxAge': 1, 'earnings': {'earningsDate': [{'raw': 1635764340, 'fmt': '2021-11-01'}, {'raw': 1636113600, 'fmt': '2021-11-05'}], 'earningsAverage': {'raw': 1.13, 'fmt': '1.13'}, 'earningsLow': {'raw': 0.97, 'fmt': '0.97'}, 'earningsHigh': {'raw': 1.27, 'fmt': '1.27'}, 'revenueAverage': {'raw': 6265160000, 'fmt': '6.27B', 'longFmt': '6,265,160,000'}, 'revenueLow': {'raw': 6041000000, 'fmt': '6.04B', 'longFmt': '6,041,000,000'}, 'revenueHigh': {'raw': 6539200000, 'fmt': '6.54B', 'longFmt': '6,539,200,000'}}, 'exDividendDate': {}, 'dividendDate': {}}}], 'error': None}}
Upvotes: 10
Views: 5269
Reputation: 200
2024 nothing works
tried
"User-agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36"
m/v7/finance/download/MSFT?period1=946677600&period2=1704060000&interval=1mo&events=history&includeAdjustedClose=true
{"finance":{"result":null,"error":{"code":"forbidden","description":"User is unable to access this feature - ...."}}}
Yahoo Finance | API Feedback We’re sorry for the inconvenience, but API-level access to Yahoo Finance quotes data has been disabled.
Yahoo Finance licenses data from 3rd-party providers that do not currently authorize us to redistribute these data in API form. Licenses that authorize redistribution come with a greater cost that varies depending on a number of factors, including whether the data is for personal or commercial use, the type of data, the volume of queries, and additional features which may be available.
We would appreciate your feedback to ensure that we can continue to serve your needs. By understanding your intended use of these API data, we will be better able to acquire the appropriate licenses. We appreciate your feedback, and we read every response.
Upvotes: 1
Reputation: 700
This worked 14-Jan-2022. I Googled "what is my user agent" to get a user agent string. By adding a browser user agent string to the request I'm mimicking being a browser.
import requests
url='https://query1.finance.yahoo.com/v7/finance/download/TSLA'
headers = {
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36'
}
r = requests.get(url, headers=headers)
print(r.status_code)
print(r.content)
Upvotes: 2
Reputation: 515
I was facing a similar problem. Apparently two things have changed:
The URL as of 6/16/2021 is:
symbolString = 'TGT'; % look up Target prices as an example
urlBase = 'https://query1.finance.yahoo.com/v7/finance/download/'; % base as of 6/16/2021
url = [urlBase,symbolString];
Then we explicitly set the user agent:
options = weboptions('UserAgent',''); # as of 6/16/2021 it is enough to submit a blank user agent
Upvotes: 9
Reputation: 108
you need to update your python package in your env:
pip install yahoo-fin -U
Upvotes: 1