python人人网遍历好友

#encode:utf-8
import urllib,urllib2,cookielib,json,time,md5,re

class  Renren(object):
    def __init__(self,email,password):
        self.email=email
        self.password=password
        self.origURL='http://www.renren.com/Home.do'
        #self.origURL='http://apps.renren.com/happyfarm/'
        self.domain='renren.com'
        self.cj = cookielib.LWPCookieJar()
        try:
            self.cj.revert('renren,cookie')
        except:
            None
        self.opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self.cj))
        urllib2.install_opener(self.opener)
  
    def login(self):
        params = {'email':self.email,'password':self.password,'origURL':self.origURL,'domain':self.domain}
        req = urllib2.Request(
            'http://www.renren.com/PLogin.do',
            urllib.urlencode(params)      
            )

          r = self.opener.open(req)
   def friends(self):
        req='http://friend.renren.com/myfriendlistx.do'
        r=self.opener.open(req)
        data = r.read()
        f=re.search('friends=\[{.*?}\];',data)
        x=f.group()
        #print x
        x=x[8:-1]
       
       
        x=x.replace('tr','Tr')
        x=x.replace('false','False')
        friends=eval(x)
      
       
       
        for i  in range(len(friends)):
           
            for key in friends[i].keys():
                if key == 'name':
                    print key,eval("u'"+friends[i][key]+"'").encode('utf-8')
                elif key == 'groups':
                    if len(friends[i][key]) != 0:
                        groups=friends[i][key][0]
                   
                        print key,eval("u'"+groups+"'").encode('utf-8')
                elif key == 'id':
                  
                  
                    req="http://www.renren.com/profile.do?id="+str(friends[i][key])
                  
                    r = self.opener.open(req)
                    print eval("u'"+friends[i]['name']+"'").encode('utf-8')
                  
                   
                else:
                     print key,friends[i][key]

a=Renren('账号,'密码')
a.login()
a.friends()
      
   

你可能感兴趣的:(python,职场,休闲)