Python site login

Tech B 0 Tallied Votes 3K Views Share

This will allow you to login a website with python. After logging in, you now have access to all the pages for "members only" accessed with python.

Could be handy for a myspace update program, webcrawlers, facebook(not recommended, facebook doesn't like scripts), and other things.

Some knowledge of HTML is recommended.

import urllib, urllib2, cookielib

#cookie storage
cj = cookielib.CookieJar()
#create an opener
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
#Add useragent, sites don't like to interact programs.
opener.addheaders.append(('User-agent', 'Mozilla/4.0'))
opener.addheaders.append( ('Referer', 'http://www.hellboundhackers.org/index.php') )

#encode the login data. This will vary from site to site.
#View the sites source code
#Example###############################################
#<form id='loginform' method='post' action='index.php'>
#<div style="text-align: center;">
#Username<br />
#<input type='text' name='user_name' class='textbox' style='width:100px' /><br />
#Password<br />
#<input type='password' name='user_pass' class='textbox' style='width:100px' /><br />
#<input type='checkbox' name='remember_me' value='y' />Remember Me<br /><br />
#<input type='submit' name='login' value='Login' class='button' /><br />

login_data = urllib.urlencode({'user_name' : 'Guest',
                               'user_pass' : 'guest_password',
                               'login' : 'Login'
                               })

resp = opener.open('http://www.somesite.org/index.php', login_data)
#you are now logged in and can access "members only" content.
#when your all done be sure to close it
resp.close()
metal_uci 0 Newbie Poster

works in joomla ???

Tech B 48 Posting Whiz in Training

I don't know what joomla is. And you will have to try it to see if it works.

AnxiousNut 0 Newbie Poster

How would that work if the login button on a webpage is actually a button and has no name for the tag? Only a class and an onclick event!

Tech B 48 Posting Whiz in Training

Just pay attention to the url and what parameters are passed during a login.

If you would give me an example site that does it that way, I will write some code to get logged in.

AnxiousNut 0 Newbie Poster

I was trying it on this
PS: the login button is an image!

<tr>
   <td class="paragraph_bold">Login Id*:</td>
</tr>
<tr>
   <td><input name="visitor/name" type="text" value="" size="15" id="visitorName" onKeyPress="return submitEnterFunction(this,event,submitLoginForm2)" /></td>
</tr>
<tr>
   <td class="paragraph_bold">Password*:</td>
</tr>
<tr>
   <td><input name="visitor/password" type="password" value="" size="15" id="visitorPassword" onKeyPress="return submitEnterFunction(this,event,submitLoginForm2)" /></td>
</tr>
<tr>
   <td><a href="#" onclick="submitLoginForm2();"><img alt="" src="/xflow/pack/docroot/themes/default/images/login.gif" border="0" class="login_btn" /></a></td>
   </tr>

As you can see, there is no name attribute! I still wanna know if there is a way!

Also i tried to do that on this site, but I've realized that the submission input tag also has no name! So what should i put instead of "login" : "value" ?

Thanks in advance!

Tech B 48 Posting Whiz in Training

Can I have a link to the site?

ultimatebuster 14 Posting Whiz in Training

I did something similar with login into SMF and a bunch of other things. *cough*

AnxiousNut 0 Newbie Poster

This is the site which has an image as the login button and the javascript as its value: http://is.gd/gLVYw

Also i tried to do that on this site, but I've realized that the submission input tag also has no name! So what should i put instead of "login" : "value" ?

I meant this site, DaniWeb

python.crawling 0 Newbie Poster

@Tech B
i'm a beginner in python crawling
i used your code snippet to login into the website "http://9gag.com/vote"
but unable to access the "members only" pages
i would be thankful to you for any help in this regard

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.