python urllib2 cookielib處理驗證碼模擬人人登陸

又重新學了下python的urllib2,參考http://www.cnpythoner.com/post/30.htmlhttp://www.pythoner.com/65.html,寫了個登陸人人的腳本,能夠處理驗證碼的情況會jpg寫到本地,主頁新鮮事正則貌似有點問題,先不管了。

# -*- coding: utf-8 -*-

import urllib
import urllib2
import cookielib
import re

import config

class Renren(object):
    def __init__(self):
        self.operate = ''  # response的對象(不含read)
        self.requestToken = self.rtk = ''
        self.icode = ''  # 驗證碼
        self.is_login = False
        #added
        self.web_content = ''
             
        self.cj = cookielib.CookieJar()
        self.opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self.cj))
        urllib2.install_opener(self.opener)
        
        self.requestToken_pattern = re.compile(r"get_check:'([-0-9]*)'")
        self.rtk_pattern = re.compile(r"get_check_x:'([a-zA-Z0-9]+)'")

    def login(self, email='', password='', origURL=''):
        postdata = {
                         'email': email,
                         'password': password,
                         'origURL': origURL,
        }

        ruid_pattern = re.compile(r"'ruid':'(\d+)'")
        failCode_pattern = re.compile(r"&failCode=(\d+)")
        
        print 'Login...'
        
        while not self.is_login:
            self.operate = self._get_response(config.LOGINURL, postdata)
            cur_url = self.operate.geturl()
            self.web_content = self.operate.read()
            #print web_content
            ruid = ruid_pattern.search(self.web_content)
            
            if ruid:
                self.is_login = True
                print u"用戶  %s %s" % (ruid.group(1), config.FAILCODE['-1'])
                return True
            else:
                failCode = failCode_pattern.search(cur_url)
                if not failCode:
                    print '無法獲得錯誤代碼'
                else:
                    definate_failCode = failCode.group(1)  # 確切的failCode字符串
                    if definate_failCode in config.FAILCODE.keys():
                        print config.FAILCODE[definate_failCode]
                        
                        if definate_failCode == '512':
                            self._get_icode_img()
                            self.icode = raw_input(u"請輸入驗證碼: ")
                            postdata['icode'] = self.icode
                            continue
                    else:
                        print '未知錯誤'
                return False
    
    def _get_response(self, url, data = None):
        if data is not None:
            req = urllib2.Request(url, urllib.urlencode(data))
        else:
            req = urllib2.Request(url)
        
        response = self.opener.open(req)
        return response
    
    def _get_requestToken(self, data):
        self.requestToken = self.requestToken_pattern.search(data).group(1)
        self.rtk = self.rtk_pattern.search(data).group(1)
    
    def _get_icode_img(self):
        icode_img = self._get_response(config.ICODEURL).read()
        self._write_file('icode.jpg', icode_img)
    
    def _write_file(self, filename, data):
        try:
            output_file = open(filename, 'wb')
            output_file.writelines(data)
            output_file.close()
            print u'文件 %s 寫入完成!' % filename
        except IOError:
            print "寫文件失敗!"

    #-------------------------------------------------------
    def viewnewinfo(self):
        """查看好友的更新狀態"""
        self.__caiinfo()

    def __caiinfo(self):
        """採集信息"""
        h3patten = re.compile('<h3>(.*?)</h3>')#匹配範圍
        apatten = re.compile('<a.+>(.+)</a>:')#匹配作者
        cpatten = re.compile('</a>(.+)\s')#匹配內容
        infocontent = self.operate.readlines()
        infocontent = self.web_content

        print 'friend newinfo:'
        #for i in infocontent:
        content = h3patten.findall(infocontent)
        if len(content) != 0:
            for m in content:
                username = apatten.findall(m)
                info = cpatten.findall(m)
                if len(username) != 0:
                    print username[0],'說',info[0]
                    print '----------------------------------------------'
                else:
                    continue


if __name__ == "__main__":
    my_account = Renren()
    my_account.login(config.EMAIL, config.PASSWORD, '')
    my_account.viewnewinfo()

config模塊代碼如下

# -*- coding: utf-8 -*-

LOGINURL = r'http://www.renren.com/PLogin.do'
ICODEURL = r'http://icode.renren.com/getcode.do?t=login&rnd=Math.random()'

EMAIL = r'你的郵箱'
PASSWORD = r'你的密碼'

# FailCode via "login-v6.js"
FAILCODE = {
            '-1': u'登錄成功',
            '0': u'登錄系統錯誤,請稍後嘗試',
            '1': u'您的用戶名和密碼不匹配',
            '2': u'您的用戶名和密碼不匹配',
            '4': u'您的用戶名和密碼不匹配',
            '8': u'請輸入帳號,密碼',
            '16': u'您的賬號已停止使用',
            '32': u'帳號未激活,請激活帳號',
            '64': u'您的帳號需要解鎖才能登錄',
            '128': u'您的用戶名和密碼不匹配',
            '512': u'請您輸入驗證碼',
            '4096': u'登錄系統錯誤,稍後嘗試',
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章