def __init__(self, username, cookiefile): Session.__init__(self, cookiefile) self.username = username res = self.request(self.mainurl) checklogin = lambda response: response.geturl().find('login') == -1 if not checklogin(res): loggedin = False while not loggedin: password = getpass('请输入 %s 的密码: ' % self.username) if not password: print('放弃登录') return logindata = { 'access': '', 'app': '', 'clientHardId': '', 'clientName': '', 'clientVer': '', 'rememberme': '1', 'to': '', 'userName': self.username, 'userPassword': password, } loggedin = self.login(self.loginurl, logindata, checklogin)