[Tutor] Web Based Form Question

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Mon, 24 Jun 2002 18:09:17 -0700 (PDT)


On Mon, 24 Jun 2002, Timothy M. Brauch wrote:

> There is a form I use on the internet that is written in Python.  Basically
> I have to enter a number in the form to look up the data on something, then
> pull the relevant data from the resulting page.
>
> What I was wondering is if there was a way I could write a little script
> that would help me do this.  If I could figure out how to get the results
> page using Python, I could easily extract the important data.
>
> Here is the relevant part of the web form...
>
> <form method="post" action="cnvrprt.py">
> <input name="idnumber" type="text">
> <input name="" type="submit" value="Submit">
> </form>


I think you can use 'urllib2' or 'httplib' to simulate a web browser doing
the submission, and then use a simple parser to extract the data.

For your script, you might be able to do something like:

###
import urllib2, urllib

def submit(url, id_number):
    """Given an url and an id_number, returns a file-like object that
holds the content of the server's response."""
    data = urllib.urlencode({'idnumber' : id_number})
    return urllib2.urlopen(url, data)
###


(I haven't tried this out though.  *grin*)  But it should be something
like that.  For more details, we can look at:

    http://www.python.org/doc/lib/module-urllib2.html



Hope this helps!