Using Python to sign into website, fill in a form, then sign out Using Python to sign into website, fill in a form, then sign out python python

Using Python to sign into website, fill in a form, then sign out


import urllibimport urllib2name =  "name field"data = {        "name" : name        }encoded_data = urllib.urlencode(data)content = urllib2.urlopen("http://www.abc.com/messages.php?action=send",        encoded_data)print content.readlines()

just replace http://www.abc.com/messages.php?action=send with the url where your form is being submitted

reply to your comment: if the url is the url where your form is located, and you need to do this just for one website, look at the source code of the page and find

<form method="POST" action="some_address.php">

and put this address as parameter for urllib2.urlopen

And you have to realise what submit button does.It just send a Http request to the url defined by action in the form.So what you do is to simulate this request with urllib2


You want the mechanize library. This lets you easily automate the process of browsing websites and submitting forms/following links. The site I've linked to has quite good examples and documentation.


You can use mechanize to work easily with this. This will ease your work of submitting the form. Don't forget to check with the parameters like name, title, message by seeing the source code of the html form.

import mechanizebr = mechanize.Browser()br.open("http://mywebsite.com/messages.php?action=send")br.select_form(nr=0)br.form['name'] = 'Enter your Name'br.form['title'] = 'Enter your Title'br.form['message'] = 'Enter your message'req = br.submit()