Python Forum

Full Version: Downloading data from Yahoo finance
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Dear Python Users,

I want to download a stock data from yahoo finance (or nasdaq.com) and use the following code. However, once I run it it gives the following message:

"ConnectionError: HTTPConnectionPool(host='ichart.finance.yahoo.com', port=80): Max retries exceeded with url: /table.csv?s=AMZN&a=8&b=1&c=2016&d=3&e=1&f=2017&g=d&ignore=.csv (Caused by NewConnectionError('<requests.packages.urllib3.connection.HTTPConnection object at 0x000000ECDE8A4A58>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed',))"

Please, help me to identify the issue.

from pandas_datareader import data as dreader
symbols = ['AMZN']
pnls = {i:dreader.DataReader(i,'yahoo','2016-09-01','2017-04-01') for i in symbols}
print(pnls.get('AMZN').head())
Here's a real time (from google ... yes it's still there) snippet that I downloaded a while back.
I just tested it and it still works!

# Obtained from the blog at: http://digitalpbk.com/stock/google-finance-get-stock-quote-realtime
import urllib.request as ur
import time


class GoogleRealTime:
    def __init__(self):
        self.prefix = "http://finance.google.com/finance/info?client=ig&q="

    def get(self, symbol, exchange):
        url = self.prefix+"%s:%s"%(exchange, symbol)
        u = ur.urlopen(url)
        content = u.read()
        print(content)
        # obj = json.loads(content[3:])
        return content


if __name__ == "__main__":
    c = GoogleRealTime()

    while 1:
        quote = c.get("MSFT", "NASDAQ")
        print (quote)
        time.sleep(30)
Quote:Max retries exceeded with url:
I can be that you're sending too many requests from same ip address in short period of time.
Here some test that you can  do.
One  requests:
>>> import pandas_datareader.data as web
>>> amzn = web.get_quote_yahoo('AMZN')
>>> amzn
         PE change_pct     last  short_ratio    time
AMZN  189.03     +0.24%  1003.74         1.52  4:00pm
The get_all_data method downloads and caches option data for all expiry months.
>>> from pandas_datareader.data import Options
>>> amzn = Options('AMZN', 'yahoo')
>>> data = amzn.get_all_data()
>>> data.iloc[0:5, 0:5]
                                             Last     Bid     Ask        Chg  \
Strike Expiry     Type Symbol                                                   
240.0  2018-01-19 call AMZN180119C00240000  764.00  766.25  770.50   0.000000   
                 put  AMZN180119P00240000    0.03    0.02    0.05   0.000000   
250.0  2018-01-19 call AMZN180119C00250000  750.90  753.00  757.05   7.200012   
                 put  AMZN180119P00250000    0.05    0.01    0.20   0.000000   
260.0  2018-01-19 call AMZN180119C00260000  734.95  727.95  730.60  11.600037   

                                             PctChg  
Strike Expiry     Type Symbol                         
240.0  2018-01-19 call AMZN180119C00240000  0.000000  
                 put  AMZN180119P00240000  0.000000  
250.0  2018-01-19 call AMZN180119C00250000  0.968134  
                 put  AMZN180119P00250000  0.000000  
260.0  2018-01-19 call AMZN180119C00260000  1.603655  
>>>