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?
You can use showforms() to list all forms once you used go… to browse to the site you want to login. Just try it from the python interpreter. note that in some cases you need to use submit().
Another way is by simply typing “chrome://settings” in your browser's address bar then hit “Enter”. Once you're on the settings page, scroll all the way down to advanced features and look for the options under “Passwords and forms”. Click “Autofill settings” and from there, you can easily turn the feature on or off.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With