Python Submit a form and get an answer

My general question is: how can I submit a form and then get a response from the site using python?

My specific: I want to send something like Ajax XHR, send to a web file and get an answer from it, is problematic.

  • I do not want to use any browser and do this in code, for example this .

  • I read these articles, and they just make me embarrassed and cannot find a good document about him.

+5
source share
2 answers

Requests are also very simple!

Here is an example from the main page related 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" }, ... } 
+5
source

Just 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() 
+2
source

All Articles