0

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?

adrCoder
  • 3,145
  • 4
  • 31
  • 56

1 Answers1

0

You'll need to import 'urllib.request' and not urllib and update your code as per below.

urllib.request.urlretrieve(make_url(ticker_symbol), make_filename(ticker_symbol, directory))

except urllib.request.ContentTooShortError as e:
import urllib.request

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.request.urlretrieve(make_url(ticker_symbol), make_filename(ticker_symbol, directory))
    except urllib.request.ContentTooShortError as e:
        outfile = open(make_filename(ticker_symbol, directory), "w")
        outfile.write(e.content)
        outfile.close()

pull_historical_data('AAPL', directory="S&P")
dswdsyd
  • 576
  • 4
  • 12
  • I get `HTTPError: Proxy Authentication Required` – adrCoder Oct 29 '19 at 10:49
  • seems like you're behind a proxy, you'll need to add the follow code to authenticate through the proxy. proxy = urllib.request.ProxyHandler({'http': r'http://username:password@url:port'}) auth = urllib.request.HTTPBasicAuthHandler() opener = urllib.request.build_opener(proxy, auth, urllib.request.HTTPHandler) urllib.request.install_opener(opener) conn = urllib.request.urlopen(make_url(ticker_symbol)) return_str = conn.read() – dswdsyd Oct 29 '19 at 11:27
  • Can you edit your reply in the post above because I don't understand what I need to change – adrCoder Oct 29 '19 at 11:33
  • Not sure whether you are aware, the ichart service has been discontinued since 2017 (https://stackoverflow.com/questions/44361361/the-ichart-finance-yahoo-com-table-csv-no-longer-working). I got a weird error message when running the code, so tried to visit the above page and got a "This site can’t be reached" message. – dswdsyd Oct 29 '19 at 12:32