automatically filling web forms?

aa8vb at yahoo.com.bbs aa8vb at yahoo.com.bbs
Mon Jul 17 07:10:02 EDT 2000


Alex:
 |Jeroen Valcke:
 |
 |> can I automatically fill webforms?
 |
 |Yes, you can.  Check out the htmllib module, which you can use to get
 |the forms out of the document, and the GET and POST capabilities of
 |urllib, which you can use to send your response.

Don't know about the htmllib module option, but here's how with urllib on
Python 1.5.2, for both GET and POST.

   import urllib, os

   BASE_URL = "http://www.a-cool-site.com:80/cgi-bin/get_data.cgi"

   parms = { 'parm1' : val1,
             'parm2' : val2 }

   if METHOD == "get":
     filename, headers = urllib.urlretrieve( "%s?%s" %
                                      ( BASE_URL, urllib.urlencode( parms ) ) )

   else: # METHOD == "post"
     #-------  I've submitted a patch so that this will work  -------
     #filename, headers = urllib.urlretrieve( BASE_URL,
     #                                        urllib.urlencode( parms ) )

     #-------  But for now, do this  -------
     fp = urllib.urlopen( BASE_URL, urllib.urlencode( parms ) )

     tfp = open(filename, 'wb')
     bs = 1024*8
     block = fp.read(bs)
     while block:
         tfp.write(block)
         block = fp.read(bs)
     tfp.close()
     fp.close()

   #=====  Work with query results here (they're in file 'filename') =====

   urllib.urlcleanup()
   if METHOD == "post":  os.unlink( filename )

--
Randall Hopper
aa8vb at yahoo.com



More information about the Python-list mailing list