Is there a way to automatically download historical prices of stocks from yahoo finance or google finance (csv format)? Preferably in Python.
You can use pandas_datareader or yfinance module to get the data and then can download or store in a csv file by using pandas. to_csv method. If yfinance is not installed on your computer, then run the below line of code from your Jupyter Notebook to install yfinance.
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
.
Short answer: Yes. Use Python's urllib to pull the historical data pages for the stocks you want. Go with Yahoo! Finance; Google is both less reliable, has less data coverage, and is more restrictive in how you can use it once you have it. Also, I believe Google specifically prohibits you from scraping the data in their ToS.
Longer answer: This is the script I use to pull all the historical data on a particular company. It pulls the historical data page for a particular ticker symbol, then saves it to a csv file named by that symbol. You'll have to provide your own list of ticker symbols that you want to pull.
import urllib base_url = "http://ichart.finance.yahoo.com/table.csv?s=" def make_url(ticker_symbol): return base_url + ticker_symbol output_path = "C:/path/to/output/directory" def make_filename(ticker_symbol, directory="S&P"): return output_path + "/" + directory + "/" + ticker_symbol + ".csv" def pull_historical_data(ticker_symbol, directory="S&P"): try: urllib.urlretrieve(make_url(ticker_symbol), make_filename(ticker_symbol, directory)) except urllib.ContentTooShortError as e: outfile = open(make_filename(ticker_symbol, directory), "w") outfile.write(e.content) outfile.close()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With