5

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.

5
  • 1
    you wanna get the response "problematically"??? What is that supposed to mean? Commented Apr 27, 2013 at 8:46
  • Tell us more. Are you trying to access a GET or POST website, does it have an API etc Commented Apr 27, 2013 at 8:51
  • I want to send or POST a XHR request to a webpage and get response which that could be save as a file with a response.I want to analyse that response in my program. Commented Apr 27, 2013 at 8:58
  • 1
    Why not just use urllib2? Commented Apr 27, 2013 at 9:01
  • Okey let my try once more.thanks. Commented Apr 27, 2013 at 9:04

2 Answers 2

7

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"
   },
  ...
}
Sign up to request clarification or add additional context in comments.

Comments

2

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()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.