Download history stock prices automatically from yahoo finance in python

后端 未结 6 1886
忘掉有多难
忘掉有多难 2020-12-04 05:42

Is there a way to automatically download historical prices of stocks from yahoo finance or google finance (csv format)? Preferably in Python.

6条回答
  •  生来不讨喜
    2020-12-04 06:17

    When you're going to work with such time series in Python, pandas is indispensable. And here's the good news: it comes with a historical data downloader for Yahoo: pandas.io.data.DataReader.

    from pandas.io.data import DataReader
    from datetime import datetime
    
    ibm = DataReader('IBM',  'yahoo', datetime(2000, 1, 1), datetime(2012, 1, 1))
    print(ibm['Adj Close'])
    

    Here's an example from the pandas documentation.

    Update for pandas >= 0.19:

    The pandas.io.data module has been removed from pandas>=0.19 onwards. Instead, you should use the separate pandas-datareader package. Install with:

    pip install pandas-datareader
    

    And then you can do this in Python:

    import pandas_datareader as pdr
    from datetime import datetime
    
    ibm = pdr.get_data_yahoo(symbols='IBM', start=datetime(2000, 1, 1), end=datetime(2012, 1, 1))
    print(ibm['Adj Close'])
    

    Downloading from Google Finance is also supported.

    There's more in the documentation of pandas-datareader.

提交回复
热议问题