under_zero
under_zero

Reputation: 31

Python simple POST method

A web page has 2 form fields

1:

<input type="text" maxlength="30" value="" name="poster" id="id_poster">

2:

<textarea name="content" cols="80" rows="20" id="id_content"></textarea>

Also it has a button:

<input type="submit" value="Submit your own idea!">

What I want is, through python to fill in the forms id_poster and id_content and then Submit. If possible, to take the webpage after submitting (to take the result).

Thank you all

Upvotes: 1

Views: 10462

Answers (2)

karlcow
karlcow

Reputation: 6972

Personally I prefer httplib2, you would have to install it. The library is a lot better than the one given that by python out of the box.

from httplib2 import Http
from urllib import urlencode
h = Http()
data = dict(id_poster="some_poster_id", id_content="some_content")
resp, content = h.request("http://example.org/form-handler", "POST", urlencode(data))

Then you can check the response with the resp

Upvotes: 7

Klaus Byskov Pedersen
Klaus Byskov Pedersen

Reputation: 121047

You can do it like this (taken from this example):

import urllib
import urllib2

url = 'http://www.someserver.com/somepage.html'
values = {'id_poster' : 'some_poster_id',
          'id_content' : 'some_content'}

data = urllib.urlencode(values)
req = urllib2.Request(url, data)
response = urllib2.urlopen(req)
the_page = response.read()

Upvotes: 4

Related Questions