#-*-Coding: UTF-8 -*-
'''
Created on 2012-7-3
@ Author: Administrator
'''
Import urllib, urllib2, cookielib, re
Class loginrenren:
Header = {'user-agent': 'mozilla/5.0 (windows; U; Windows NT 6.1; RV: 1.9.1.6) Gecko/20091201 Firefox/3.5.6 '}
Email =''
Password =''
Cookie = none
Cookiefile = './cookie. dat'
Friendlist = []
Def _ init _ (self, email, passwd ):
Self. Email = Email
Self. Password = passwd
# Cookie
Self. Cookie = cookielib. lwpcookiejar ()
Opener = urllib2.build _ opener (urllib2.httpcookieprocessor (self. Cookie ))
Urllib2.install _ opener (opener)
Def login (Self ):
Postdata = {
'Email ': Self. email,
'Password': Self. Password,
'Origin': 'http: // www.renren.com/home ',
'Key _ id': '1'
}
Postdata = urllib. urlencode (postdata)
# Log on
Req = urllib2.request (
Url = 'HTTP: // www.renren.com/ajaxlogin/login ',
Data = postdata,
Headers = self. Header
)
Result = urllib2.urlopen (req). Read ()
Self. Cookie. Save (self. cookiefile)
Result = STR (result)
Print result
If "true" in result:
Print 'login successful... '
Else:
Print "Logon failed... "
Exit (1)
Def getfriendlist (Self ):
Req = urllib2.request (
Url = 'HTTP: // friend.renren.com/myfriendlistx.do ',
Headers = self. Header
)
Result = urllib2.urlopen (req). Read ()
Self. Cookie. Save (self. cookiefile)
Friend = STR (Re. Search ('ds DS = \ [{. *} \] ', result). Group ())
Friendid = Re. findall (R' "ID ":(.*?),. *?, "Name ":"(.*?) "', Friend)
For f in friendid:
Self. friendlist. append (f)
Print "% s" % F [1]. Decode ('unicode-escape ')
If _ name _ = '_ main __':
User = loginrenren ("XXX@163.com", "xxxxxx ")
User. login ()
User. getfriendlist ()
Reprinted from: http://www.oschina.net/code/snippet_181057_11612