script to download Yahoo Finance data

John Hunter jdhunter at ace.bsd.uchicago.edu
Thu Jul 1 19:10:15 EDT 2004


>>>>> "dan" == dan roberts <bro092 at yahoo.com> writes:

    dan> Folks, This is my first Python project so please bear with
    dan> me. I need to download data from Yahoo Finance in CSV
    dan> format. The symbols are provided in a text file, and the
    dan> project details are included below.  Does anyone have some
    dan> sample code that I could adapt?

In the matplotlib finance module there is some code to get historical
quotes from yahoo.  I'll repost the relevent bit here - but you can
grab the src distribution from http://matplotlib.sf.net and look in
matplotlib/finance.py for more info

"converter" is defined in the matplotlib.dates module and is used to
convert the data to and from a date time instance, eg epoch, mx
datetimes or python2.3 datetimes.  If you don't need that
functionality it is easy to strip from the function below

def quotes_historical_yahoo(ticker, date1, date2,
                            converter=EpochConverter()):

    """
    Get historical data for ticker between date1 and date2.  converter
    is a DateConverter class appropriate for converting your dates

    results are a list of

    d, open, close, high, low, volume
    

    where d is an instnace of your datetime supplied by the converter
    """

    y,m,d = converter.ymd(date1)
    d1 = (m, d, y)
    y,m,d = converter.ymd(date2)
    d2 = (m, d, y)

    urlFmt = 'http://table.finance.yahoo.com/table.csv?a=%d&b=%d&c=%d&d=%d&e=%d&f=%d&s=%s&y=0&g=d&ignore=.csv'
    url =  urlFmt % (d1[0], d1[1], d1[2],
                     d2[0], d2[1], d2[2], ticker)

    ticker = ticker.upper()

    results = []
    try:
        lines = urlopen(url).readlines()
    except IOError, exc:
        print 'urlopen() failure\n' + url + '\n' + exc.strerror[1]
        return None
        
    for line in lines[1:]:

        vals = line.split(',')

        if len(vals)!=7: continue
        datestr = vals[0]

        d = converter.strptime(datestr, '%d-%b-%y')
        open, high, low, close =  [float(val) for val in vals[1:5]]
        volume = int(vals[5])

        results.append((d, open, close, high, low, volume))
    results.reverse()
    return results




More information about the Python-list mailing list