Unable to read S&P500 using pandas_datareader on Google Finance

吃可爱长大的小学妹 提交于 2019-12-11 06:15:40

问题


Goal:

I'm trying to read historical data from Google Finance for the S&P500 using pandas_datareader.

Issue

I'm using the symbol on google finance but I keep getting the same error "unable to read url". I pretty sure I'm putting in the wrong symbol but I've tried all the options I know and I don't know what else to do. What symbol can I use with pandas_datareader to pull the historical data for the S&P 500? Any help would be greatly appreciated.

Results

Results & Errors

Code

import urllib, sys, pandas, pandas_datareader
from yahoo_finance import Share
from bs4 import BeautifulSoup
from pandas_datareader import data
import fix_yahoo_finance
from datetime import datetime

def Main():
    datetime_start = datetime(2010, 1, 1)
    datetime_finish = datetime(2013, 1, 27)

    f_google = data.DataReader('F', 'google', datetime_start, datetime_finish)
    print('Ford on google, symbol "F", on 04 January 2010: %s' %f_google.loc['2010-01-04'])

    sp_500_yahoo = Share('^GSPC')
    print('Yahoo S&P 500: %s' %sp_500_yahoo.get_price())

    list_symbols = ['INDEXSP:.INX', 'INDEXSP', '.INX', 'INDEXSP:', '^GSPC', 'SPX']

    for string_symbol in list_symbols:
        try:
            sp500_google = data.DataReader(string_symbol, 'google', start = datetime_start, end = datetime_finish)
            print('\nS&P 500 on google, symbol %s, on 04 January 2010: %s' %(string_symbol, sp500_google.loc['2010-01-04']))
            except Exception as e:
                string_error = str(e.args)
                print('\nError for S&P 500 symbol %s on google: %s' %(string_symbol, string_error))
        else:
           pass
        finally:
           pass

     # system info
     print('\npython version: %s' %sys.version)
     print('pandas version: %s' %pandas.__version__)
     print('pandas_datareader version: %s' %pandas_datareader.__version__)
     print('fix_yahoo_finance version: %s' %fix_yahoo_finance.__version__)

if __name__ == '__main__':
     Main()

来源:https://stackoverflow.com/questions/44235964/unable-to-read-sp500-using-pandas-datareader-on-google-finance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!