How can I mimic CGI form submission (METHOD=POST)?
I would like to retrieve web pages that are the result of POSTing a form. Is there existing code that would let me do this easily?
Yes. Here’s a simple example that uses urllib2.
import urllib import urllib2 uri = "http://www.example.com/query" params = dict(activity="inquisition", origin="Spain") try: f = urllib2.urlopen(uri, urllib.urlencode(params)) except urllib2.URLError, e: print "Failed:", e else: print "Succeeded:", f.read()
The form parameters are added to a dictionary, which is then encoded and passed as the second argument to urllib2.urlopen. The response can then be read as usual.