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.
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"
},
...
}
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With