總得思路很簡單:
擷取一個cookie
裝載好自己的request
發送請求
#coding:utf8 '''類比登陸人人 根據網上的資料和firefox做了下 首先自己去探查了下頁面元素:<input id="email" class="input-text" type="text" value="" tabindex="1" name="email" style="color: rgb(136, 136, 136);"></input<input id="password" class="input-text" type="password" autocomplete="off" tabindex="2" error="請輸入密碼" name="password"></input>cookie: jebecookies=523a9b12-658f-43c0-abf8-1ca1f3f87c10|||||; domain=.renren.com; path=/'''import re,urllib,urllib2,cookielib,codecs,chardet,sys class LoginRenRen(): def __init__(self,name='',password='',domain=''): self.name=name self.password=password self.domain=domain self.cj = cookielib.LWPCookieJar() try: #自動去遠程擷取一個cookie #self.cj.revert('renren.coockie') #測試的時候每次都有異常但是毫無影響,注釋掉了也不影響 print 'successed got a cookie..' except Exception,e: print 'Can not get the cookies',e.message #裝載cookies self.opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self.cj)) urllib2.install_opener(self.opener) def login(self): params = {'domain':self.domain,'email':self.name,'password':self.password} req = urllib2.Request( 'http://www.renren.com/PLogin.do', urllib.urlencode(params) ) print 'login.....' self.openrate = self.opener.open(req) #查看下返回的url來判斷登陸進去了沒 print self.openrate.geturl() #info = self.openrate.read() #查看了一下頁面編碼 chardet是第三方庫 #print chardet.detect(info) print '' #print re.findall(r'password',info) #列印出了返回的內容 type = sys.getfilesystemencoding() #print info.decode("UTF-8").encode(type) if __name__=='__main__': username = 'liuzhizhi123@126.com' #使用者名稱 password = '.....' #密碼 domain = 'renren.com' ren = LoginRenRen(username,password,domain) ren.login()