自动登录SNS

1.自动登录人人网:

import re,time,sys

from urllib import request, parse

from http import cookiejar



class renren():

    def __init__(self,email,password):

        self.url='http://www.renren.com/'

        self.email=email

        self.password=password

        self.cookie=cookiejar.LWPCookieJar()

        self.opener=request.build_opener(request.HTTPCookieProcessor(self.cookie))

        self.friendIdList=[]

    def login(self):

        url=self.url+'PLogin.do/'

        params=parse.urlencode({'email':self.email,'password':self.password})

        response=self.opener.open(request.Request(url,params.encode("utf-8")))

        #print(str(response.readline().decode('utf-8')), str(response.readline().decode('utf-8')))

        text = open("text.html","wb")

        #for i in range(10):

            #print(str(response.readline().decode('utf-8')))

        buf = response.read().decode('utf-8')

        text.write(buf.encode('utf-8'))

        url=self.url+'home'

        if response.geturl()==url:

            print ("Login Succesfully!")

            self.cookie.save('xiaonei.coockie')

        else:

            print ('Login Failed')

    def changeState(self):

        pass



    def getFriendList(self):

        url='http://friend.renren.com/myfriendlistx.do'

        response=self.opener.open(request.Request(url))

        html=response.read().decode('utf-8')

        friend=str(re.search('friends=\[{.*}\]',html).group())

        friendId=re.findall('"id":\d+',friend)

        for id in friendId:

            self.friendIdList.append(id.lstrip('"id":'))

        print (len(self.friendIdList))

    def browseFriend(self):

        cnt=0

        for friendId in self.friendIdList:

            cnt+=1

            url='http://www.renren.com/profile.do?id='+'%s'%(friendId)

            request.Request(url)

            time.sleep(1)

            

if __name__=='__main__':

    print(sys.getdefaultencoding())

    app=renren('username','password')

    app.login()

    app.changeState()

    app.getFriendList()

    app.browseFriend()

你可能感兴趣的:(SNS)