Retrieving a stock quote using Python?
Suchandra Thapa
ssthapa at classes.cs.uchicago.edu
Thu May 22 12:36:34 EDT 2003
David Lees <abcdebl2nonspammy at verizon.net> wrote:
> I would like to write a utility in Python that takes a stock symbol as
> an input and returns a current price. Suggestions on how to do this are
> welcome. I was thinking of an approach that would go out to a page like
> finance.yahoo.com, entering the stock symbol and then collecting the
> results and parsing them for the price. But it has been awhile since I
> used pythton and I'm not sure I ever really knew how to do this.
Try this little script that retrives stock quotes from MSN:
#!/usr/bin/env python
import urllib2,re
def getQuote(symbol):
"""Get the current price for a given stock ticker symbol"""
url = "http://moneycentral.msn.com/scripts/"
url += "webquote.dll?ipage=qd&Symbol=%s" % symbol
urlData = urllib2.urlopen(url)
quotePage = urlData.read()
priceRegex = r"Last.*?<TD.*?><B> (\d+.\d+)</B></TD>"
priceMatch = re.search(priceRegex, quotePage, re.DOTALL)
price = float(priceMatch.group(1))
return price
It works now, however if MSN changes their page a lot, it may break.
--
----------------------------------------------------------------------------
Suchandra Thapa
s-thapa-11 at NOSPAMalumni.uchicago.edu
----------------------------------------------------------------------------
More information about the Python-list
mailing list