script to download Yahoo Finance data

wes weston wweston at att.net
Fri Jul 2 04:09:53 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?

This is really weird. The following test is run 4 times with no modification
and the output shown. As you can see, only two results are the same. Yet,
if you go to the yahoo historical data and download the file, the file
is ok?

Think I'll try another source of data.
wes

import urllib

def GetHistoricalDailyDataForOneSymbol( symbol, day1, day2):
     #day1 = "2001-01-01"
     y1 = day1[:4]
     #m1 = int(day1[5:7]) - 1
     m1 = int(day1[5:7])
     d1 = day1[8:]

     y2 = day2[:4]
     #m2 = int(day2[5:7]) - 1
     m2 = int(day2[5:7])
     d2 = day2[8:]

     url_str = "http://ichart.yahoo.com/table.csv?s=%s&a=%d&b=%s&c=%s&d=%d&e=%s&f=%s&x=.csv"\
     % (symbol.upper(),m1,d1,y1,  m2,d2,y2  )


     f = urllib.urlopen(url_str)
     lines = f.readlines()
     f.close()
     return lines


#--------------------------------------------------------------------
if __name__ == "__main__":
     d1 = '2004-05-01'
     d2 = '2004-06-01'
     list = GetHistoricalDailyDataForOneSymbol('jkhy',d1,d2)
     print d1,d2,"num=",len(list)
     print list[1][:-1],"  to"
     print list[-2][:-1]



 >>>
2004-05-01 2004-06-01 num= 59
21-Jun-04,19.32,19.50,19.32,19.42,350200,19.42   to
30-Mar-04,19.06,19.30,18.91,19.30,113100,19.26
 >>> ================================ RESTART ================================
 >>>
2004-05-01 2004-06-01 num= 59
21-Jun-04,19.32,19.50,19.32,19.42,350200,19.42   to
30-Mar-04,19.06,19.30,18.91,19.30,113100,19.26
 >>> ================================ RESTART ================================
 >>>
2004-05-01 2004-06-01 num= 67
1-Jul-04,20.08,20.13,19.80,19.88,352900,19.88   to
30-Mar-04,19.06,19.30,18.91,19.30,113100,19.26
 >>> ================================ RESTART ================================
 >>>
2004-05-01 2004-06-01 num= 66
30-Jun-04,19.63,20.10,19.53,20.10,348900,20.10   to
30-Mar-04,19.06,19.30,18.91,19.30,113100,19.26
 >>>




More information about the Python-list mailing list