def __get_api(self): url = "https://passport.baidu.com/v2/api/?getapi&tpl=mn&apiver=v3&tt={tt}&class=login&gid={gid}&logintype=dialogLogin&callback=bd__cbs__tesnqc".format( tt=base.get_time_stamp(), gid=self.gid) temp_text = self.session.get(url).text # 由于百度返回的结果并不是标准的Json格式,所以无奈用正则进行匹配 token = re.search('"token" : "([a-fA-F0-9]+?)"', temp_text).group(1) self.token = token
def __get_public_key(self): headers = default_conf.base_headers url = "https://passport.baidu.com/v2/getpublickey?token={token}&tpl=mn&apiver=v3&tt={tt}&gid={gid}&callback=bd__cbs__9t0drq".format( token=self.token, tt=base.get_time_stamp(), gid=self.gid) temp_text = self.session.get(url, headers=headers).text re_result = re.search( '"pubkey":\'(?P<pubkey>[\s\S]+?)\',"key":\'(?P<key>.+?)\'', temp_text) self.key = re_result.group("key") self.pubkey = re_result.group("pubkey").replace("\\n", "\n") key = rsa.PublicKey.load_pkcs1_openssl_pem(self.pubkey) self.rsaed_password = base64.b64encode( rsa.encrypt(self.password.encode("utf-8"), key))
def __login_check(self): # 有一个参数叫dv,但是不知道为什么没有这个参数也正常获得结果 headers = default_conf.base_headers url = "https://passport.baidu.com/v2/api/?logincheck&token={token}&tpl=mn&apiver=v3&tt={tt}&sub_source=leadsetpwd&username={username}&isphone={is_phone}&dv={dv}&callback=bd__cbs__sehp6m".format( tt=base.get_time_stamp(), is_phone=False, token=self.token, username=self.user_name, dv="") temp_text = self.session.get(url, headers=headers).text re_result = re.search( '"codeString" : "(?P<code_string>.+?)".*?"vcodetype" : "(?P<v_code_type>.+?)"', temp_text) self.code_string = re_result.group("code_string") self.v_code_type = re_result.group("v_code_type")
def __login(self, verify_code): data = { 'staticpage': "https://www.baidu.com/cache/user/html/v3Jump.html", 'charset': "UTF-8", "token": self.token, "tpl": 'mn', "subpro": "", "apiver": "v3", "tt": base.get_time_stamp(), "codestring": self.code_string, "safeflg": "0", 'u': "https://www.baidu.com/", "isPhone": "", "detect": "1", # 不清楚什么意思 'gid': self.gid, "quick_user": "******", "logintype": "dialogLogin", 'logLoginType': "pc_loginDialog", 'idc': "", 'loginmerge': "true", 'splogin': "******", "username": self.user_name, "password": self.rsaed_password, "verifycode": verify_code, "mem_pass": "******", "rsakey": self.key, 'crypttype': "12", 'ppui_logintime': "10797", 'countrycode': "", 'dv': "", 'callback': "parent.bd__pcbs__r6aj37" } headers = default_conf.base_headers headers["Host"] = "passport.baidu.com" url = "https://passport.baidu.com/v2/api/?login" self.session.post(url, data=data, headers=headers)
def __login_history(self): headers = default_conf.base_headers url = "https://passport.baidu.com/v2/api/?loginhistory&token={token}&tpl=mn&apiver=v3&tt={tt}&gid={gid}&callback=bd__cbs__splnc1".format( token=self.token, tt=base.get_time_stamp(), gid=self.gid) self.session.get(url=url, headers=headers)