Reputation: 181
My general question : How could i submit a form and then get response from website with a python program ?
My specific : I want to send some thing like Ajax XHR send to a web file and get response from it , problematically .
I don't want to use any browser and do it in the code like this link.
I have read this articles and they just make me confused and can't find good documented about it.
Upvotes: 5
Views: 15262
Reputation: 11367
Requests is very easy too!
Here is the example from their homepage pertaining to POSTing forms
>>> payload = {'key1': 'value1', 'key2': 'value2'}
>>> r = requests.post("http://httpbin.org/post", data=payload)
>>> print r.text
{
...
"form": {
"key2": "value2",
"key1": "value1"
},
...
}
Upvotes: 7
Reputation: 3630
Simply use urllib2
import urllib
import urllib2
data = {
'field1': 'value1',
'field2': 'value2',
}
req = urllib2.Request(url="http://some_url/...",
data=urllib.urlencode(data),
headers={"Content-type": "application/x-www-form-urlencoded"})
response = urllib2.urlopen(req)
the_page = response.read()
Upvotes: 2