1

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

Klaus Byskov Pedersen
  • 117,245
  • 29
  • 183
  • 222
under_zero
  • 31
  • 1
  • 2
  • 6
  • 2
    so duplicated... http://stackoverflow.com/questions/1082361/automatically-pressing-a-submit-button-using-python, http://stackoverflow.com/questions/393738/programmatic-form-submit. Just in a 10-sec search. – tokland Feb 12 '11 at 18:11

2 Answers2

7

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

karlcow
  • 6,977
  • 4
  • 38
  • 72
4

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()
Klaus Byskov Pedersen
  • 117,245
  • 29
  • 183
  • 222