Python

Greg Krohn infinitystwin.SPAM at IS.BAD.yahoo.com
Thu Jan 31 23:16:13 EST 2002


"your_name" <your_email_address at upenn.edu> wrote in message
news:3C59FF86.9B211598 at upenn.edu...
> I'm trying to write a Python script that looks up the current value of
> the NASDAQ on 2 different webpages and compares the values.  I'm having
> some difficulties in figuring out how to grab the index values off of
> the websites.  The codes i'm familar with are below.  any suggestions?
>
>
> import urllib
> cnnnasdaq="http://money.cnn.com/markets/nasdaq.html"
> cnnpage=urllib.urlopen(cnnnasdaq).read()
>
>
> sandpindex=re.search(r'(S&P 500)(.*?)(\d+\d+)', data,
> re.IGNORECASE|re.DOTALL)

First off, that's got to be the most descriptive subject line ever. ;)

If Emile's re-free code doesn't float your boat, try this:

import re, urllib
from xml.sax.saxutils import escape

def getmarket(symbol):
    symbol = escape(symbol)
    pat = re.compile(r'"charttext">%s</a>.*?"chartdata">(\d+(.\d+)*)' %
symbol,
                     re.IGNORECASE|re.DOTALL)
    match =
pat.search(urllib.urlopen("http://money.cnn.com/markets/nasdaq.html
").read())
    if match:
        return match.group(1)
    else:
        return None

print getmarket('s&p 500')


greg





More information about the Python-list mailing list