How can I login to a website with Python?


How can I do it? I was trying to enter some specified link (with urllib), but to do it, I need to log in.

I have this source from the site:

<form id="login-form" action="auth/login" method="post">
    <!--label for="rememberme">Remember me</label><input type="checkbox" class="remember" checked="checked" name="remember me" /-->
    <label for="email" id="email-label" class="no-js">Email</label>
    <input id="email-email" type="text" name="handle" value="" autocomplete="off" />
    <label for="combination" id="combo-label" class="no-js">Combination</label>
    <input id="password-clear" type="text" value="Combination" autocomplete="off" />
    <input id="password-password" type="password" name="password" value="" autocomplete="off" />
    <input id="sumbitLogin" class="signin" type="submit" value="Sign In" />

Is this possible?

8/11/2017 1:16:27 PM

Accepted Answer

Maybe you want to use twill (it's based on mechanize). It's quite easy to use and should be able to do what you want.

It will look like the following:

from twill.commands import *

fv("1", "email-email", "")
fv("1", "password-clear", "testpass")


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.

1/8/2013 9:35:30 AM

Let me try to make it simple, suppose URL of the site is and you need to sign up by filling username and password, so we go to the login page say now and view it's source code and search for the action URL it will be in form tag something like

 <form name="loginform" method="post" action="userinfo.php">

now take userinfo.php to make absolute URL which will be '', now run a simple python script

import requests
url = ''
values = {'username': 'user',
          'password': 'pass'}

r =, data=values)
print r.content

I Hope that this helps someone somewhere someday.

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow