activating a webpage's click command from python

Peter Hansen peter at
Mon Jun 10 14:06:14 EDT 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 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")

import urllib
import re
import sys

URL = ''
pattern = '%s</a>.*?<b>([0-9.]*)</b>'
symbol = sys.argv[1].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[0])
    print 'Undefined result: %s' % (matches,)

NT.TO last trade was 2.69
C:\>python ibm
IBM last trade was 78.03
C:\>python engc
Undefined result: []

Hmm... Engenuity Corporation doesn't seem to be listed yet. :-)


More information about the Python-list mailing list