script to download Yahoo Finance data

wes weston wweston at att.net
Wed Jun 30 19:36:31 CEST 2004


dan roberts wrote:
> Folks,
> 
> This is my first Python project so please bear with me. I need to
> download data from Yahoo Finance in CSV format. The symbols are
> provided in a text file, and the project details are included below.
> Does anyone have some sample code that I could adapt?
> 
> Many thanks in advance,
> dan
> 
> /*---NEED TO DO------*/
> Considering IBM as an example, the steps are as follows.
> 
> A. Part 1 - download 'Historical Prices' from
> http://finance.yahoo.com/q?s=ibm
> 1. Get the Start Date from the form at the top of this page,
> http://finance.yahoo.com/q/hp?s=IBM
> (I can provide the end date.)
> 2. Click on Get Prices
> 3. Then finally click on Download to Spreadsheet and save the file
> with a name like IBM_StartDate_EndDate.csv.
> (2) and (3) are equivalent to using this link directly,
> http://ichart.yahoo.com/table.csv?s=IBM&a=00&b=2&c=1962&d=05&e=30&f=2004&g=d&ignore=.csv
> Can you please post an example of a loop that would do the above for a
> series of company symbols, saved in a text file?
> 
> B. Part 2 - download 'Options' from http://finance.yahoo.com/q?s=ibm
> This seems more difficult because the data is in html format (there's
> no option to download CSV files). What's the easiest/best way to take
> care of this?

Dan,
    Note the parser funtion is not used here but,
might come in handy.
wes
#--------------------------------------------------------------------------
import tkSimpleDialog
import tkFileDialog
import sys

#--------------------------------------------------------------------------
def GetHistoricalDailyDataForOneSymbol( symbol, day1, day2):
     """
     Download the yahoo historical data as a comma separated file.
     """
     #day1 = "2001-01-01"
     #        0123456789
     y1 = day1[:4]
     m1 = day1[5:7]
     d1 = day1[8:]
     y2 = day2[:4]
     m2 = day2[5:7]
     d2 = day2[8:]
     url_str = "http://chart.yahoo.com/table.csv?s=" + symbol + \
                                                    "&a=" + m1 + "&b=" + d1 + "&c=" + y1 + \
                                                    "&d=" + m2 + "&e=" + d2 + "&f=" + y2 + \
                                                    "&g=d&q=q&y=0" + "&z=" + symbol.lower() + "&x=.csv"
     f = urllib.urlopen(url_str)
     lines = f.readlines()
     f.close()
     return lines
#--------------------------------------------------------------------
def GetStockHistFile(symbol,earlyDate,lateDate,fn):
     list = GetHistoricalDailyDataForOneSymbol( symbol, earlyDate, lateDate)
     if (not list) or (len(list) < 1):
         return 0
     fp    = open( fn,"w" )
     for line in list[1:]:
         fp.write( line )
     fp.close()
     return len(list)
#--------------------------------------------------------------------
def ParseDailyDataLine(line):
     """ 25-Jan-99,80.8438,81.6562,79.5625,80.9375,25769100
         22-Jan-99,77.8125,80.1172,77.625,78.125,20540000
         21-Jan-99,80.875,81.6562,78.875,79.1562,20019300
         20-Jan-99,83.4688,83.875,81.2422,81.3125,31370300
         19-Jan-99,75.6875,79.1875,75.4375,77.8125,25685400
     """
     if line[0] == '<':
         return None
     list = string.split(line,",")
     pos  = 0;
     for str in list: #skip header
         #print "str=",str,"pos=",pos
         if pos == 0: #"9-Jan-01"
             try:
                 list1 = string.split( str, "-" )
                 day   = int(list1[0])
                 month = list1[1]
                 month = int(MonthStringToMonthInt( month ))
                 year  = int( list1[2] )
                 if year < 70:    # oh well, it will work for 70 years or until yahoo changes
                     year += 2000 # year is 101 here for a string input of 01
                 else:
                     year += 1900
                 date = "%d-%02d-%02d" % (year, month, day) #mx.DateTime.DateTime( year, month, day  )
                 #println( "date=" + Date.toString() ); // this "2001-01-05"
             except:
                 print "error in ParseDailyDataLine"
                 print "line=["+str+"]"
         elif pos == 1:
             Open   = WES.MATH.MyMath.Round(float( str ),4)
         elif pos == 2:
             High   = WES.MATH.MyMath.Round(float( str ),4)
         elif pos == 3:
             Low    = WES.MATH.MyMath.Round(float( str ),4)
         elif pos == 4:
             Close  = WES.MATH.MyMath.Round(float( str ),4)
         elif pos == 5:
             Volume = long  ( str )
         elif pos == 6:
             AdjClose = WES.MATH.MyMath.Round(float( str ),4)
         else:
             print "ret none 1"
             return None
         pos += 1

     if pos == 7:
         return (date,Open,High,Low,Close,Volume)
     else:
         print "ret none 2"
         return None
#--------------------------------------------------------------------
if __name__ == '__main__':
     str = tkSimpleDialog.askstring("","enter <symbol> <early date> <late date>")
     if not str:
         sys.exit(1) #return
     list      = str.split()
     symbol    = list[0]
     earlyDate = list[1]
     lateDate  = list[2]
     fn = tkFileDialog.asksaveasfilename()
     if not fn:
         sys.exit(1) #return
     if GetStockHistFile(symbol,earlyDate,lateDate,fn):
         tkMessageBox.showinfo("Added",symbol )
     else:
         tkMessageBox.showinfo("Error Adding",symbol )




More information about the Python-list mailing list