[Tutor] retrieving stock quotes
Timothy Wilson
wilson@visi.com
Mon, 10 Dec 2001 21:57:55 -0600 (CST)
On Mon, 10 Dec 2001, Timothy Wilson wrote:
> Does anyone know of an ftp site or Web site that has fairly current stock
> quotes in an easily parsable format?
<lame self-reply>
Very cool! I happened upon exactly what I needed. Go to
http://finance.yahoo.com/ and enter a ticker symbol (RHAT, for
example). Notice the 'Download Spreadsheet' link below the quote
table? That's a link to a simple comma-separated list of values. Bingo!
Here's some sample code.
wilson@copland:~$ python2.1
Python 2.1.1 (#1, Nov 11 2001, 18:19:24)
[GCC 2.95.4 20011006 (Debian prerelease)] on linux2
Type "copyright", "credits" or "license" for more information.
>>> import urllib2
>>> base = 'http://finance.yahoo.com/d/quotes.csv?s='
>>> tail = '&f=sl1d1t1c1ohgv&e=.csv'
>>> ticker = 'RHAT'
>>> url = base+ticker+tail
>>> url
'http://finance.yahoo.com/d/quotes.csv?s=RHAT&f=sl1d1t1c1ohgv&e=.csv'
>>> quote = urllib2.urlopen(url).read()
>>> quote
'"RHAT",7.76,"12/10/2001","3:59PM",-0.20,7.85,8,7.65,1520500\r\n'
>>>
Now you can slice and dice at will and pull out all sorts of good info. My
students will have fun with this.
</lame self-reply>
-Tim
--
Tim Wilson | Visit Sibley online: | Check out:
Henry Sibley HS | http://www.isd197.org | http://www.zope.com
W. St. Paul, MN | | http://slashdot.org
wilson@visi.com | <dtml-var pithy_quote> | http://linux.com