async def handle_200(request, response): user = request.match_info.get("userKey") or 'x' product = request.match_info.get("productKey") or 'x' recommend = response.text or 'x' success_log(user, product, recommend) return response
def __do_exam(self, driver): if None == driver: print('剩余答题次数为0') return error_cnt = 0 #获取下一题按钮 next_prob_btn = driver.find_element_by_class_name('w_btn_tab_down') for i in range(self.question_num): prob = self.__get_question(driver, i) ans = self.question_bank.get_answer(prob) #首先在原题库中寻找答案,如找不到则使用模糊查找,两者都找不到返回'E' # “我要的并不在这里,你给的答案没意义,都选C,都选C~” # --大鹏 if 'E' == ans[0]: ans = self.question_bank.get_ans_fuzzy(prob) if 'E' == ans[0]: error_cnt += 1 ans[0] = 'C' ans = self.__ans_dropout(ans) radio_checker = driver.find_elements_by_name("%s%s" % ('ra_', i)) for item in ans: radio_checker[ord(item) - ord('A')].click() sleep(1) if i != self.question_num - 1: next_prob_btn.click() sleep(2) # get submit btn submit_btn = driver.find_element_by_class_name('W_fr') submit_btn.click() sleep(2) confirm_btn = driver.find_element_by_class_name('btn-danger') confirm_btn.click() log.success_log('score', str((20 - error_cnt) * 5)) sleep(2) '''
def __init_user_info(self): #从文件读取 data = easy_read.read_from_utf8(self.login_record_file_path) if data is None: last_pos = -1 last_md5 = 0 else: #local_file文件格式:第一行记录文件md5值,第二行记录上次答题最后答题用户的位置 lines = data.split('\n') last_md5 = lines[0] if len(lines) == 2: last_pos = int(lines[1]) else: last_pos = -1 #计算当前user_info文件的md5值 new_md5 = self.calc_md5(self.user_info_file_path) self.md5 = new_md5 #新旧user_info不同,为了保证将所有用户都答题,讲user_info中的用户全部重新答 if last_md5 != new_md5: self.pos = -1 log.success_log('new', '') log.error_log('', 'new') else: self.pos = last_pos data = easy_read.read_from_utf8(self.user_info_file_path) lines = data.split('\n') for line in lines: #跳过空行 if len(line) == 0: continue username, password = line.split(' ') self.user_info.append([username, password])
def login(self, username, password, driver): #登录三次失败后,放弃本次登录 err_login_cnt = 0 driver.get('http://xxjs.dtdjzx.gov.cn/') sleep(2) # 关闭提示框 cancel_tip = driver.find_elements_by_xpath( "//body/div[@id='myxiaoxi']/div/div/div[@class='modal-header']/button" ) if cancel_tip[0]: cancel_tip[0].click() sleep(2) #点击左侧竞赛答题 driver.find_element_by_id('lbuts').click() sleep(2) #选择党员身份 shenfen = driver.find_element_by_id('shenfen') shenfen.find_element_by_xpath("//option[@value='0']").click() btn_confirm = driver.find_element_by_id('bts') btn_confirm.click() while err_login_cnt < 3: #填入用户名密码验证码 driver.find_element_by_id('username').send_keys(username) sleep(1) driver.find_element_by_id('password').send_keys(password) sleep(1) val_code = self.recognize.recognize(driver) driver.find_element_by_id("validateCode").send_keys(val_code) sleep(1) #点击登录 driver.find_element_by_class_name('js-submit').click() sleep(3) #若登录失败,增加失败计数,从新开始登录 if ('https://sso.dtdjzx.gov.cn/sso/login?error' == driver.current_url): err_login_cnt += 1 continue #关闭提示框 cancel_tip = driver.find_elements_by_xpath( "//body/div[@id='myxiaoxi']/div/div/div[@class='modal-header']/button" ) if cancel_tip[0]: cancel_tip[0].click() sleep(2) #登录成功后获取答题次数 opportunity = driver.find_element_by_class_name('l_jihui') #剩余答题次数为0,不进行后续操作 if '0' == opportunity.text[2]: driver.quit() return None driver.find_element_by_id("lbuts").click() sleep(3) log.success_log('username', username) return driver #登录错误次数超过三次,可认为登录错误归因于用户名密码错误 driver.quit() log.error_log(username, 'error username/password!') return None
__name__ = 'test' if __name__ == "__main__": user_list, last_pos = auto_login.get_user_info_from_txt() print(last_pos) for pos in range(last_pos + 1, len(user_list)): user = user_list[pos] driver = auto_login.login_with_chrome(user[0], user[1]) auto_login.record_pos_2_local(pos) if None == driver: log.error_log(user[0], ' login error!') continue elif 'finish' == driver: log.success_log(user[0], ' has finish the quiz!') continue score = do_quiz.do_practice(driver) log.success_log(user[0], score) driver.quit() sleep(1) print('all user has finish the quiz') if __name__ == 'file_test': user_list, last_pos = auto_login.get_user_info_from_txt(path='utf8.txt') for pos in range(last_pos + 1, len(user_list)):