Python _ automatically logs on to Renren to output a friend list

Source: Internet
Author: User

#-*-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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.