You are not logged in.

#1 2008-12-19 20:04:57

Boris Bolgradov
Member
From: Bulgaria
Registered: 2008-07-27
Posts: 185

Login a website from Python

A friend of mine asked me if I could make a script to log into a website and print some information.

So, let's say I want the script to log into the ArchLinux forum (http://bbs.archlinux.org/login.php) and print the page I'm redirected to. How can I do this? I searched in Google, but I couldn't find anything that can help me. Please, can you make the script for me and explain me how it's working? Thank you in advance.

- Boris

Offline

#2 2008-12-19 20:20:52

pointone
Wiki Admin
From: Waterloo, ON
Registered: 2008-02-21
Posts: 379

Re: Login a website from Python

I'd recommend using the ClientForm Python module. The examples on their website are quite thorough.


M*cr*s*ft: Who needs quality when you have marketing?

Offline

#3 2008-12-19 20:52:43

elmer_42
Member
From: /na/usa/ca
Registered: 2008-10-11
Posts: 427

Re: Login a website from Python

I've logged into sites from the command line before. I use curl to do so. Usually it's something like `curl -F 'email=example@epicsauce.com' -F 'pass=ihaveaweakpassword' http://epicsauce.com/login.php`.


[ lamy + pilot ] [ arch64 | wmii ] [ ati + amd ]

Offline

#4 2008-12-19 22:03:28

catwell
Member
From: Bretagne, France
Registered: 2008-02-20
Posts: 207
Website

Re: Login a website from Python

A tool well-known by Perl users but that also exists in Python can do almost anything on a website; it's called mechanize.

EDIT: I didn't know about ClientForm but it looks that it's almost the same thing (same website)! Anyway that's the way to go.

Last edited by catwell (2008-12-19 22:04:46)

Offline

#5 2008-12-19 22:07:10

Boris Bolgradov
Member
From: Bulgaria
Registered: 2008-07-27
Posts: 185

Re: Login a website from Python

Using ClientForm, I can type the username and password and log into this forum, but I'm stuck at the redirect page, because I'm not being redirected and I don't know how to get to the index with keeping me logged in. (I tried opening a direct link to the index, but urllib2 doesn't save cookies, I think).

And the website my friend wants to log into - SecondLife.com - I type the first name, last name, and password, but when I do

print urlopen(form.click()).read()

to print the page, it prints the login page... I believe, it just reloads the page.

I need help. Thanks.

- Boris

Last edited by Boris Bolgradov (2008-12-20 08:30:44)

Offline

#6 2008-12-20 09:04:07

Boris Bolgradov
Member
From: Bulgaria
Registered: 2008-07-27
Posts: 185

Re: Login a website from Python

I searched Google for handling cookies and redirects with urllib2. Problem solved.

Offline

#7 2009-01-03 10:08:40

jakadinho
Member
From: Kranj, Slovenia
Registered: 2008-10-25
Posts: 32
Website

Re: Login a website from Python

Do you mind sharing the script or whatever you made?

Im planing to do something similar and i think something to start with would be a great help.

Offline

#8 2009-01-03 14:33:47

Boris Bolgradov
Member
From: Bulgaria
Registered: 2008-07-27
Posts: 185

Re: Login a website from Python

jakadinho wrote:

Do you mind sharing the script or whatever you made?

Im planing to do something similar and i think something to start with would be a great help.

Sure. Here it is:

import urllib2, cookielib, re
import ClientForm

class SecondLife:
    def __init__(self, usernames, password):
        self.username, self.lastname = usernames.split(' ')      # split First_Name and Last_Name
        self.password = password
        self.url = 'https://secondlife.com/account/friends.php?lang=en'

        cookiejar = cookielib.LWPCookieJar()
        cookiejar = urllib2.HTTPCookieProcessor(cookiejar)
        # debugger = urllib2.HTTPHandler(debuglevel=1)

        opener = urllib2.build_opener(cookiejar)
        urllib2.install_opener(opener)

    def login(self):
        response = urllib2.urlopen(self.url)
        forms = ClientForm.ParseResponse(response, backwards_compat=False)

        # forms[0] is 'GET', forms[1] is 'POST'
        form = forms[1]

        try:
            form['form[username]'] = self.username
            form['form[lastname]'] = self.lastname
            form['form[password]'] = self.password
        except Exception, e:
            print 'The following error occured: \n"%s"' % e
            print
            print 'A good idea is to open a browser and see if you can log in from there.'
            print 'URL:', self.url
            raw_input()
            exit()

        self.page = urllib2.urlopen(form.click('submit')).read()

    def friends_online(self):
        self.login()

        pattern = re.compile('.*-color:.*;">(.*)</li>')
        friends_online = pattern.findall(self.page)

        for friend in friends_online:
            print friend


SL = SecondLife('First_Name Last_Name', 'Password')
SL.friends_online()

raw_input()

Offline

#9 2009-01-03 15:36:51

timetrap
Member
From: Here and There
Registered: 2008-06-05
Posts: 342
Website

Re: Login a website from Python

Thanks! I was making a screen scraper for someone but got stuck at the login.

Offline

Board footer

Powered by FluxBB