activating a webpage's click command from python
peter at engcorp.com
Mon Jun 10 20:06:14 CEST 2002
"Grimes, Jessica C" wrote:
> I want to create a program that will ask the user what stock he/she
> wants to look up. The program will then extract financial data from
> yahoo and import it into a spreadsheet.
> My problem is that I don't know how to make the application take the
> user's input (the stock's ticker symbol) and place the user's input
> from python into the space where yahoo accepts input for a ticker
> symbol and activate yahoo's "Look up" click command.
You should investigate HTML forms. The web page contains a form
which has several fields and a target URL. If you view the source
for the page (in Netscape, hit Ctrl-U; in IE use the View->Source
menu) you can find buried within it the form and the names of the
fields. Forms use the <form> tag and contain <input> fields.
Looking at finance.yahoo.com I can see the form calls "/q" when
executed, and has a field named "s" for the stock symbol. You
can easily take such forms and compose a URL which retrieves the
data. In this case this:
does the job for Nortel on the TSE (disclaimer: anyone even
thinking of buying Nortel should probably just keep thinking :-).
The page returned contains the data, but it's embedded deep
in the HTML and you _really_ don't want to waste much of your
time trying to figure this out. There's got to be a better
approach, but I have never spent the time to research it.
Anyway, the following script does the job in a Pythonically
pragmatic way (in this case, that means "just barely")
URL = 'http://finance.yahoo.com/q?s=%s'
pattern = '%s</a>.*?<b>([0-9.]*)</b>'
symbol = sys.argv.upper()
data = urllib.urlopen(URL % symbol).read()
matches = re.findall(pattern % symbol, data)
if len(matches) == 1:
print '%s last trade was %s' % (symbol, matches)
print 'Undefined result: %s' % (matches,)
C:\>python tick.py nt.to
NT.TO last trade was 2.69
C:\>python tick.py ibm
IBM last trade was 78.03
C:\>python tick.py engc
Undefined result: 
Hmm... Engenuity Corporation doesn't seem to be listed yet. :-)
More information about the Python-list