views:

38

answers:

1

First of all, sorry if this question is a little vague and rambling! I'm ok with Python, but I've never done anything HTTP related before.

I'm trying to automate submitting a web form, and from reading some of this page I understand that I need to do a POST request. I also found a code snippet demonstrating the urllib module:


import urllib
params = urllib.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})
f = urllib.urlopen("http://www.musi-cal.com/cgi-bin/query", params)
print f.read()

But I still don't really understand what I'm doing. I need to trigger "submit" somehow, and I assume the actual data I'm submitting will go in the params somewhere?

A: 

The code there should do what you want.

Whatever data you want to use should go into the params as you have in your example. When the params are included as an argument to urlopen a POST request will be used (instead of a GET).

By just calling urlopen I believe the POST request will be submitted. If you want the response however you will need to use f.read().

Chris Daviduik
cool, that's easy. and the submit button would be part of the url?
Jeff
Yes: look in the html constituting the submit button--there will be an "action" attribute with a string constituting the url you must send your encoded request to.
twneale
yeah, that's what i expected. the page i'm trying to use is more complicated i think (some javascript and stuff), but it's good to know i'm approaching things right. thanks!
Jeff