Golix
Golix

Reputation: 181

Python Submit a form and get response

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 .

Upvotes: 5

Views: 15262

Answers (2)

Nipun Batra
Nipun Batra

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

onon15
onon15

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

Related Questions