31

As part of my quest to become better at Python I am now attempting to sign in to a website I frequent, send myself a private message, and then sign out. So far, I've managed to sign in (using urllib, cookiejar and urllib2). However, I cannot work out how to fill in the required form to send myself a message.

The form is located at /messages.php?action=send. There's three things that need to be filled for the message to send: three text fields named name, title and message. Additionally, there is a submit button (named "submit").

How can I fill in this form and send it?

Matthew
  • 2,232
  • 4
  • 23
  • 37
  • possible duplicate of [Fill form values in a web page via a Python script (not testing)](http://stackoverflow.com/questions/1555234/fill-form-values-in-a-web-page-via-a-python-script-not-testing) – ravi404 Jan 23 '14 at 08:26

5 Answers5

16

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 mechanize
br = 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()
Prashant Dey
  • 580
  • 5
  • 16
16
import urllib
import urllib2

name =  "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

Jan Vorcak
  • 19,261
  • 14
  • 54
  • 90
  • Whoop, sorry. It's the page where the form is located. (continued in next comment thanks to my silly iPod) – Matthew Dec 19 '11 at 12:04
  • (continued) Thanks! That looks like it will work. I'll try it in the morning - will that submit the form, too, or do I have to put something in the data list for "submit"? – Matthew Dec 19 '11 at 12:05
14

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.

Andrew Wilkinson
  • 10,682
  • 3
  • 35
  • 38
  • :D I've got it installed now, I'll have a play around with it! Thanks :D – Matthew Dec 20 '11 at 02:32
  • I am getting this error when I try to import mechanize: Traceback (most recent call last): File "", line 1, in import mechanize File "C:\Users\Admin\Desktop\WinPython-64bit-3.6.1.0Qt5\python-3.6.1.amd64\lib\site-packages\mechanize\__init__.py", line 119, in from _version import __version__ ModuleNotFoundError: No module named '_version' – girl101 Mar 15 '18 at 10:23
3

Try to work out the requests that are made (e.g. using the Chrome web developer tool or with Firefox/Firebug) and imitate the POST request containing the desired form data.

In addition to the great mechanize library mentioned by Andrew, in case I'd also suggest you use BeautifulSoup to parse the HTML.

If you don't want to use mechanize but still want an easy, clean solution to create HTTP requests, I recommend the excellend requests module.

Danilo Bargen
  • 18,626
  • 15
  • 91
  • 127
2

To post data to webpage, use cURL something like this,

curl -d Name="Shrimant" -d title="Hello world" -d message="Hello, how are you" -d Form_Submit="Send" http://www.example.com/messages.php?action=send

The “-d” option tells cURL that the next item is some data to be sent to the server at http://www.example.com/messages.php?action=send

SMshrimant
  • 638
  • 9
  • 15