stock quotes

Bryan belred at gmail.com
Thu Sep 14 00:50:06 EDT 2006


Donlingerfelt wrote:
> I would like to download stock quotes from the web, store them, do
> calculations and sort the results.  However I am fairly new and don't have a
> clue how to parse the results of a web page download.   I can get to the
> site, but do not know how to request the certain data  need.  Does anyone
> know how to do this?  I would really appreciate it.  Thanks.
> 
> 

i recently wrote a moinmoin macro which i believe does exactly what you want. 
even though you aren't writing a moinmoin macro, all the stuff you need is here.

usage: [[stock(amzn,goog,yhoo)]]

note that the string 'amzn,goog,yahoo' is passed in as the symbols variable and 
is placed as-is onto the url.  you will receive one .csv file from yahoo with 
*all* the ticker info for all symbols you requested... very cool :)  then for 
each row in the csv file, i pull out each column (data) and set a red or green 
color based on whether the stock is up or down for the day as well as providing 
a link to the yahoo finance site (finance.google.com in my latest version) when 
that symbol is clicked.  and finally return an html table with the data.

i hope this helps you.  i apologize in advance if this code doesn't come through 
the newsgroup formatted properly.



import urllib
import csv
def execute(macro, symbols):
     color = 'black'
     try:
         reader = 
csv.reader(urllib.urlopen('http://finance.yahoo.com/d/quotes.csv?s=%s&f=sl1d1t1c1ohgv&e=.csv' 
% symbols))
         data = []
         for symbol, trade, date, time, change, opened, hi, low, volume in reader:
             num = float(change)
             if num > 0:
                 color = 'green'
             elif num < 0:
                 color = 'red'
             percent = 100 * float(change) / (float(trade) - float(change))
             data.append('<tr><td><a 
href="http://finance.yahoo.com/q?s=%s">%s</a></td><td><font color="%s">%s (%s / 
%.2f%%)</font></td></tr>' % (symbol, symbol, color, trade, change, percent))
         return '<table>%s</table>' % ''.join(data)
     except:
         return '%s: Stock information unavailable' % symbols



bryan




More information about the Python-list mailing list