You are not logged in.
Pages: 1
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
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
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
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
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
I searched Google for handling cookies and redirects with urllib2. Problem solved.
Offline
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
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
Thanks! I was making a screen scraper for someone but got stuck at the login.
Offline
Pages: 1