Beispiel #1
0
 def login(self):
     html = self.session.get(self.login_url, verify=False).text
     soup = BeautifulSoup(html, 'lxml')
     form = soup.select('#casLoginForm')
     if (len(form) == 0):
         raise Exception('出错啦!网页中没有找到casLoginForm')
     # 填充数据
     params = {}
     form = soup.select('input')
     for item in form:
         if None != item.get('name') and len(item.get('name')) > 0:
             if item.get('name') != 'rememberMe':
                 if None == item.get('value'):
                     params[item.get('name')] = ''
                 else:
                     params[item.get('name')] = item.get('value')
     salt = soup.select("#pwdDefaultEncryptSalt")
     if (len(salt) != 0):
         salt = salt.get_text()
     else:
         pattern = '\"(\w{16})\"'
         salt = re.findall(pattern, html)
         if (len(salt) == 1):
             salt = salt[0]
         else:
             salt = False
     params['username'] = self.username
     if not salt:
         params['password'] = self.password
     else:
         params['password'] = Utils.encryptAES(self.password, salt)
         if self.getNeedCaptchaUrl():
             imgUrl = self.host + 'authserver/captcha.html'
             code = Utils.getCodeFromImg(self.session, imgUrl)
             params['captchaResponse'] = code
     data = self.session.post(self.login_url,
                              params=params,
                              allow_redirects=False)
     # 如果等于302强制跳转,代表登陆成功
     if data.status_code == 302:
         jump_url = data.headers['Location']
         self.session.post(jump_url, verify=False)
         return self.session.cookies
     elif data.status_code == 200:
         data = data.text
         soup = BeautifulSoup(data, 'lxml')
         msg = soup.select('#errorMsg')[0].get_text()
         raise Exception(msg)
     else:
         raise Exception('教务系统出现了问题啦!返回状态码:' + str(data.status_code))
 def login(self):
     html = self.session.get(self.login_url, verify=False).text
     soup = BeautifulSoup(html, 'lxml')
     form = soup.select('#loginFromId')
     # 有两种登陆方式,这里选择第二种
     if (len(form) != 2):
         raise Exception('出错啦!网页中没有找到登陆表单')
     form = form[1]
     html = "<html>" + str(form) + "</html>"
     soup = BeautifulSoup(html, 'lxml')
     form = soup.select('input')
     # 填充数据
     params = {}
     form = soup.select('input')
     for item in form:
         if None != item.get('name') and len(item.get('name')) > 0:
             if item.get('name') != 'rememberMe':
                 if (None == item.get('value')):
                     params[item.get('name')] = ''
                 else:
                     params[item.get('name')] = item.get('value')
     salt = soup.select("#pwdEncryptSalt")
     if len(salt) == 0:
         raise Exception('未找到salt,请联系开发者')
     salt = salt[0].get('value')
     params['username'] = self.username
     params['password'] = Utils.encryptAES(self.password, salt)
     if self.getNeedCaptchaUrl():
         imgUrl = self.host + 'authserver/getCaptcha.htl'
         code = Utils.getCodeFromImg(self.session, imgUrl)
         params['captcha'] = code
     data = self.session.post(self.login_url,
                              params=params,
                              allow_redirects=False)
     if data.status_code == 302:
         jump_url = data.headers['Location']
         self.session.post(jump_url, verify=False)
         return self.session.cookies
     elif data.status_code == 200:
         data = data.text
         soup = BeautifulSoup(data, 'lxml')
         msg = soup.select('#showErrorTip')[0].get_text()
         raise Exception(msg)
Beispiel #3
0
 def login(self):
     html = self.session.get(self.login_url, verify=False).text
     soup = BeautifulSoup(html, 'lxml')
     form = soup.select('#loginFromId')
     if len(form) == 0:
         raise Exception('出错啦!网页中没有找到LoginForm,这是程序的问题!')
     # 填充input字段
     params = {}
     form = soup.select('#loginFromId input')
     for item in form:
         if None != item.get('name') and len(item.get('name')) > 0:
             if item.get('name') != 'rememberMe':
                 if (None == item.get('value')):
                     params[item.get('name')] = ''
                 else:
                     params[item.get('name')] = item.get('value')
     params.pop('dllt')
     # 寻找salt
     salt = soup.select('#pwdEncryptSalt')[0].get('value')
     # 填充用户名和密码
     params['username'] = self.username
     params['password'] = Utils.encryptAES(self.password, salt)
     if self.getNeedCaptchaUrl():
         imgUrl = self.host + '/authserver/getCaptcha.htl'
         code = Utils.getCodeFromImg(self.session, imgUrl)
         params['captcha'] = code
     res = self.session.post(self.login_url,
                             params=params,
                             verify=False,
                             allow_redirects=False)
     if res.status_code == 302:
         jump_url = res.headers['Location']
         self.session.post(jump_url, verify=False)
         print(f'已经从教务系统获取到Cookies:{self.session.cookies}')
         return self.session.cookies
     elif res.status_code == 401:
         msg = soup.select('#formErrorTip2')[0].get_text()
         raise Exception(msg)
     else:
         raise Exception('教务系统出现了问题啦!返回状态码:' + str(res.status_code))
Beispiel #4
0
 def login(self):
     params = {}
     self.ltInfo = self.session.post(f'{self.host}iap/security/lt',
                                     data=json.dumps({})).json()
     params['lt'] = self.ltInfo['result']['_lt']
     params['rememberMe'] = 'false'
     params['dllt'] = ''
     params['mobile'] = ''
     params['username'] = self.username
     params['password'] = self.password
     needCaptcha = self.getNeedCaptchaUrl()
     if needCaptcha:
         imgUrl = f'{self.host}iap/generateCaptcha?ltId={self.ltInfo["result"]["_lt"]}'
         code = Utils.getCodeFromImg(self.session, imgUrl)
         params['captcha'] = code
     else:
         params['captcha'] = ''
     data = self.session.post(f'{ self.host }iap/doLogin',
                              params=params,
                              verify=False,
                              allow_redirects=False)
     if data.status_code == 302:
         data = self.session.post(data.headers['Location'], verify=False)
         return self.session.cookies
     else:
         data = data.json()
         self.count += 1
         if data['resultCode'] == 'CAPTCHA_NOTMATCH':
             if self.count < 10:
                 self.login()
             else:
                 raise Exception('验证码错误超过10次,请检查')
         elif data['resultCode'] == 'FAIL_UPNOTMATCH':
             raise Exception('用户名密码不匹配,请检查')
         else:
             raise Exception(f'登陆出错,状态码:{ data["resultCode"]},请联系开发者修复...')