I am trying to run the code from here:
Download history stock prices automatically from yahoo finance in python
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:/Users/test/Desktop/research/stock_data/test"
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()
pull_historical_data('AAPL', directory="S&P")
But I get the following error:
AttributeError: module 'urllib' has no attribute 'ContentTooShortError'
How can I make it work? Or can you point me to some code that works?