def zhuanxiang(cookies, scores): if scores["zhuanxiang"] < const.zhuanxiang_all: # driver_zhuanxiang = Mydriver(nohead=nohead) time.sleep(random.randint(5, 15)) driver_zhuanxiang = Mydriver(nohead=False) driver_zhuanxiang.driver.maximize_window() print('请保持窗口最大化') print('请保持窗口最大化') print('请保持窗口最大化') driver_zhuanxiang.get_url("https://www.xuexi.cn/notFound.html") driver_zhuanxiang.set_cookies(cookies) try_count = 0 if scores["zhuanxiang"] < const.zhuanxiang_all: letters = list("ABCDEFGHIJKLMN") driver_zhuanxiang.get_url( 'https://pc.xuexi.cn/points/my-points.html') driver_zhuanxiang.click_xpath( '//*[@id="app"]/div/div[2]/div/div[3]/div[2]/div[7]/div[2]/div[2]/div' ) time.sleep(2) # for tem in range(0, 40): # try: # temword = driver_zhuanxiang.driver.find_element_by_xpath( # '//*[@id="app"]/div/div[2]/div/div[4]/div/div/div/div[' + str(tem + 1) + ']/div[2]/button').text # except: # temword = '' # name_list = ["开始答题", "继续答题"] # , "重新答题" # if (any(name in temword for name in name_list)): # driver_zhuanxiang.click_xpath( # '//*[@id="app"]/div/div[2]/div/div[4]/div/div/div/div[' + str(tem + 1) + ']/div[2]/button') # break dati = driver_zhuanxiang.driver.find_elements_by_css_selector( "#app .items .item button") toclick = dati # print("专项答题列表长度:",len(toclick)) for i in range(len(dati) - 1, -1, -1): # 从最后一个遍历到第一个 j = dati[i] if ("重新" in j.text or "满分" in j.text): continue else: toclick = j toclick.click() break while scores["zhuanxiang"] < const.zhuanxiang_all: try: category = driver_zhuanxiang.xpath_getText( '//*[@id="app"]/div/div[2]/div/div[6]/div[1]/div[1]' ) # get_attribute("name") except Exception as e: print('查找元素失败!') break print(category) tips, tip_full_text = driver_zhuanxiang._view_tips() check_delay() if not tips: print("本题没有提示") if "填空题" in category: print('没有找到提示,暂时略过') continue elif "多选题" in category: print('没有找到提示,暂时略过') continue elif "单选题" in category: print('没有找到提示,暂时略过') continue # return driver_daily._search(driver_daily.content, driver_daily.options, driver_daily.excludes) else: print("题目类型非法") break else: if "填空题" in category: answer = tips driver_zhuanxiang.zhuanxiang_fill_in_blank(answer) elif "多选题" in category: options = driver_zhuanxiang.radio_get_options() radio_in_tips, radio_out_tips = "", "" for letter, option in zip(letters, options): for tip in tips: if tip in option: # print(f'{option} in tips') if letter not in radio_in_tips: radio_in_tips += letter radio_out_tips = [ letter for letter, option in zip(letters, options) if (letter not in radio_in_tips) ] print('含 ', radio_in_tips, '不含', radio_out_tips) if len( radio_in_tips ) > 1: # and radio_in_tips not in driver_zhuanxiang.excludes: print('根据提示', radio_in_tips) driver_zhuanxiang.radio_check(radio_in_tips) elif len(radio_out_tips ) > 1: # and radio_out_tips not in excludes print('根据提示', radio_out_tips) driver_zhuanxiang.radio_check(radio_out_tips) # return driver_zhuanxiang._search(content, options, excludes) else: print('无法根据提示判断,准备搜索……') elif "单选题" in category: options = driver_zhuanxiang.radio_get_options() if '因此本题选' in tips: check = [x for x in letters if x in tips] driver_zhuanxiang.radio_check(check) else: radio_in_tips, radio_out_tips = "", "" ''' option_elements = driver_zhuanxiang.wait.until(driver_zhuanxiang.EC.presence_of_all_elements_located( (driver_zhuanxiang.By.XPATH, '//*[@id="app"]/div/div[2]/div/div[4]/div[1]'))) # option_elements = self.find_elements(rules['challenge_options']) options = [x.get_attribute("name") for x in option_elements]''' for letter, option in zip(letters, options): for tip in tips: if tip in option: # print(f'{option} in tips') if letter not in radio_in_tips: radio_in_tips += letter else: # print(f'{option} out tips') if letter not in radio_out_tips: radio_out_tips += letter print('含 ', radio_in_tips, '不含', radio_out_tips) if 1 == len( radio_in_tips ): # and radio_in_tips not in driver_zhuanxiang.excludes: print('根据提示', radio_in_tips) driver_zhuanxiang.radio_check(radio_in_tips) elif 1 == len( radio_out_tips ): # and radio_out_tips not in excludes print('根据提示', radio_out_tips) driver_zhuanxiang.radio_check(radio_out_tips) # return driver_zhuanxiang._search(content, options, excludes) else: print('无法根据提示判断,准备搜索……') else: print("题目类型非法") break time.sleep(1) total, scores = show_score(cookies) if scores["zhuanxiang"] >= const.zhuanxiang_all: print("检测到专项答题分数已满,退出学习") driver_zhuanxiang.quit() try: driver_zhuanxiang.quit() except Exception as e: print('浏览器退出出了一点小问题...') else: print("专项答题之前学完了")
def refresh_all_cookies(live_time=8.0, display_score=False): # cookie有效时间保持在live_time以上 template_json_str = '''{}''' cookies_json_obj = file.get_json_data("user/cookies.json", template_json_str) need_check = False valid_cookies = [] for uid in cookies_json_obj: cookies_b64 = cookies_json_obj[uid] cookies_bytes = base64.b64decode(cookies_b64) cookie_list = pickle.loads(cookies_bytes) for d in cookie_list: # 检查是否过期 if 'name' in d and 'value' in d and 'expiry' in d and d[ "name"] == "token": remain_time = (int(d['expiry']) - (int)(time.time())) / 3600 print( color.green(uid + "_" + get_nickname(uid) + ",登录剩余有效时间:" + str(int(remain_time * 1000) / 1000) + " 小时."), end="") if remain_time < 0: print(color.red(" 已过期 需要重新登陆,将自动移除此cookie.")) remove_cookie(uid) else: # print(color.blue(" 有效"), end="") valid_cookies.append(cookie_list) if remain_time <= live_time: # 全新cookies的有效时间是12h print(color.red(" 需要刷新")) need_check = True # 暂没有证据表明可以用requests来请求,requests请求的响应不带cookies,不确定会不会更新cookies时间 # (但是万一服务端自动更新了cookie,可以试试12h之后再访问呢?则剩余时间直接设为12即可。有空的伙计可以做个实验) # jar = RequestsCookieJar() # for cookie in cookie_list: # jar.set(cookie['name'], cookie['value']) # new_cookies = requests.get("https://pc.xuexi.cn/points/my-points.html", cookies=jar, # headers={'Cache-Control': 'no-cache'}).cookies.get_dict() # 浏览器登陆方式更新cookie,速度较慢但可靠 driver_login = Mydriver(nohead=False) driver_login.get_url( "https://www.xuexi.cn/notFound.html") driver_login.set_cookies(cookie_list) driver_login.get_url( 'https://pc.xuexi.cn/points/my-points.html') new_cookies = driver_login.get_cookies() driver_login.quit() found_token = False for j in new_cookies: # 检查token if 'name' in j and j["name"] == "token": found_token = True if not found_token: remove_cookie(uid) # cookie不含token则无效,删除cookie else: save_cookies(new_cookies) else: print(color.green(" 无需刷新")) if need_check: # 再执行一遍来检查有效情况 print("再次检查cookies有效时间...") refresh_all_cookies() elif display_score: for cookie in valid_cookies: user_id = get_userId(cookie) print(color.blue(get_fullname(user_id)) + " 的今日得分:") score.show_score(cookie)
def daily(cookies, scores): if scores["daily"] < const.daily_all: # driver_daily = Mydriver(nohead=nohead) time.sleep(random.randint(5, 15)) driver_daily = Mydriver(nohead=False) driver_daily.driver.maximize_window() print('请保持窗口最大化') print('请保持窗口最大化') print('请保持窗口最大化') driver_daily.get_url("https://www.xuexi.cn/notFound.html") driver_daily.set_cookies(cookies) try_count = 0 if scores["daily"] < const.daily_all: letters = list("ABCDEFGHIJKLMN") driver_daily.get_url('https://pc.xuexi.cn/points/my-points.html') driver_daily.click_xpath( '//*[@id="app"]/div/div[2]/div/div[3]/div[2]/div[5]/div[2]/div[2]/div' ) #点击每日答题的去答题按钮 while scores["daily"] < const.daily_all: try: category = driver_daily.xpath_getText( #获取题目类型 '//*[@id="app"]/div/div[2]/div/div[4]/div[1]/div[1]' ) # get_attribute("name") except Exception as e: print('查找题目类型...查找元素失败!') break ans_results = driver_daily.driver.find_elements_by_css_selector( ".practice-result .infos .info") if (len(ans_results) != 0): #已经找到答题结果页面 print(ans_results[0].get_attribute("innerHTML")) print(ans_results[0].text) print(ans_results[2].get_attribute("innerHTML")) print(ans_results[2].text) exit(2) break print(category) log_daily("\n====================") log_daily(log_timestamp()) log_daily("【" + category + "】") log_daily("【题干】") q_body = driver_daily.driver.find_element_by_css_selector( ".q-body") q_html = q_body.get_attribute('innerHTML') q_text = q_body.text print(q_text) log_daily(q_html) tips, tip_full_text = driver_daily._view_tips() log_daily("【提示信息】") log_daily(str(tips) + "\n" + tip_full_text) if not tips: print("本题没有提示") log_daily("!!!!!本题没有找到提示,暂时略过!!!!!") if "填空题" in category: print('没有找到提示,暂时略过') continue elif "多选题" in category: print('没有找到提示,暂时略过') continue elif "单选题" in category: print('没有找到提示,暂时略过') continue # return driver_daily._search(driver_daily.content, driver_daily.options, driver_daily.excludes) else: print("题目类型非法") log_daily("!!!!!无提示,题目类型非法!!!!!") break else: if "填空题" in category: answer = tips driver_daily.fill_in_blank(answer) elif "多选题" in category: options = driver_daily.radio_get_options() log_daily("【多选题选项】") log_daily(str(options)) radio_in_tips, radio_out_tips = "", "" for letter, option in zip(letters, options): for tip in tips: if tip in option: # print(f'{option} in tips') if letter not in radio_in_tips: radio_in_tips += letter radio_out_tips = [ letter for letter, option in zip(letters, options) if (letter not in radio_in_tips) ] print('包含提示的选项 ', radio_in_tips, ',不包含提示的选项 ', radio_out_tips) log_daily('包含提示的选项 ' + str(radio_in_tips) + ',不包含提示的选项 ' + str(radio_out_tips)) if len( radio_in_tips ) > 1: # and radio_in_tips not in driver_daily.excludes: print('根据提示', radio_in_tips) driver_daily.radio_check(radio_in_tips) elif len(radio_out_tips ) > 1: # and radio_out_tips not in excludes print('根据提示', radio_out_tips) driver_daily.radio_check(radio_out_tips) # return driver_daily._search(content, options, excludes) else: print('无法根据提示判断,请自行答题……') log_daily("!!!!!无法根据提示判断,请自行答题……!!!!!") elif "单选题" in category: options = driver_daily.radio_get_options() log_daily("【单选题选项】") log_daily(str(options)) if '因此本题选' in tips: #提示类型1 check = [x for x in letters if x in tips] log_daily("根据提示类型1,选择答案:" + str(check)) driver_daily.radio_check(check) else: radio_in_tips, radio_out_tips = "", "" ''' option_elements = driver_daily.wait.until(driver_daily.EC.presence_of_all_elements_located( (driver_daily.By.XPATH, '//*[@id="app"]/div/div[2]/div/div[4]/div[1]'))) # option_elements = self.find_elements(rules['challenge_options']) options = [x.get_attribute("name") for x in option_elements]''' for letter, option in zip(letters, options): for tip in tips: if tip in option: # print(f'{option} in tips') if letter not in radio_in_tips: radio_in_tips += letter else: # print(f'{option} out tips') if letter not in radio_out_tips: radio_out_tips += letter print('包含提示的选项 ', radio_in_tips, ',不包含提示的选项 ', radio_out_tips) log_daily('包含提示的选项 ' + str(radio_in_tips) + ',不包含提示的选项 ' + str(radio_out_tips)) if 1 == len( radio_in_tips ): # and radio_in_tips not in driver_daily.excludes: print('根据提示', radio_in_tips) driver_daily.radio_check(radio_in_tips) elif 1 == len( radio_out_tips ): # and radio_out_tips not in excludes print('根据提示', radio_out_tips) driver_daily.radio_check(radio_out_tips) # return driver_daily._search(content, options, excludes) else: print('无法根据提示判断,请自行答题……') log_daily("!!!!!无法根据提示判断,请自行答题……!!!!!") else: print("题目类型非法") log_daily("!!!!!有提示,但题目类型非法!!!!!") break time.sleep(1) total, scores = show_score(cookies) if scores["daily"] >= const.daily_all: print("检测到每日答题分数已满,退出学习") try: driver_daily.quit() except Exception as e: print('driver_daily quit fail...') else: print("每日答题之前学完了")
def weekly(cookies, scores): if scores["weekly"] < const.weekly_all: # driver_weekly = Mydriver(nohead=nohead) time.sleep(random.randint(5, 15)) driver_weekly = Mydriver(nohead=False) driver_weekly.driver.maximize_window() print('请保持窗口最大化') print('请保持窗口最大化') print('请保持窗口最大化') driver_weekly.get_url("https://www.xuexi.cn/notFound.html") driver_weekly.set_cookies(cookies) try_count = 0 if scores["weekly"] < const.weekly_all: letters = list("ABCDEFGHIJKLMN") driver_weekly.get_url('https://pc.xuexi.cn/points/my-points.html') driver_weekly.click_xpath( '//*[@id="app"]/div/div[2]/div/div[3]/div[2]/div[6]/div[2]/div[2]/div' ) time.sleep(2) # <<<<<<< fix-some-bugs # flag = 1 # for tem in range(0, 40): # for tem2 in range(0, 5): # try: # temword = driver_weekly.driver.find_element_by_xpath( # '//*[@id="app"]/div/div[2]/div/div[4]/div/div[' + str(tem + 1) + ']/div[2]/div[' + str( # tem2 + 1) + ']/button').text # except: # temword = '' # name_list = ["开始答题", "继续答题"] # if flag == 1 and (any(name in temword for name in name_list)): # driver_weekly.click_xpath( # '//*[@id="app"]/div/div[2]/div/div[4]/div/div[' + str(tem + 1) + ']/div[2]/div[' + str( # tem2 + 1) + ']/button') # flag = 0 dati = driver_weekly.driver.find_elements_by_css_selector( "#app .month .week button") toclick = dati for i in range(len(dati) - 1, -1, -1): j = dati[i] if ("重新" in j.text or "满分" in j.text): continue else: toclick = j toclick.click() break while scores["weekly"] < const.weekly_all and try_count < 10: # ''' # ======= # flag = 1 # page_num = 1 # last_page = int(driver_weekly.driver.find_element_by_xpath('//*[@id="app"]/div/div[2]/div/div[5]/ul/li[last()-1]/a').text) # while page_num < last_page and flag == 1: # print('进入每周答题第'+ str(page_num) +'页') # all_month = len(driver_weekly.driver.find_elements_by_class_name('month')) # cur_month = 1 # for tem in range(0, all_month): # for tem2 in range(0, 6): # if flag == 0: # break # try: # temword = driver_weekly.driver.find_element_by_xpath( # '//*[@id="app"]/div/div[2]/div/div[4]/div/div[' + str(tem + 1) + ']/div[2]/div[' + str( # tem2 + 1) + ']/button').text # except: # temword = '' # if all_month == cur_month: # driver_weekly.click_xpath( # '//*[@id="app"]/div/div[2]/div/div[5]/ul/li[' + str(page_num + 2) + ']') # print('切换至下一页') # page_num += 1 # time.sleep(2) # cur_month += 1 # break # name_list = ["开始答题", "继续答题"] # if flag == 1 and (any(name in temword for name in name_list)): # driver_weekly.click_xpath( # '//*[@id="app"]/div/div[2]/div/div[4]/div/div[' + str(tem + 1) + ']/div[2]/div[' + str( # tem2 + 1) + ']/button') # flag = 0 # elif '重新答题' in temword: # continue # while each[6] < 5 and try_count < 10: # >>>>>>> dev # ''' try: category = driver_weekly.xpath_getText( '//*[@id="app"]/div/div[2]/div/div[4]/div[1]/div[1]' ) # get_attribute("name") except Exception as e: print('查找元素失败!') break print(category) tips, tip_full_text = driver_weekly._view_tips() check_delay() if not tips: print("本题没有提示") if "填空题" in category: print('没有找到提示,暂时略过') continue elif "多选题" in category: print('没有找到提示,暂时略过') continue elif "单选题" in category: print('没有找到提示,暂时略过') continue # return driver_daily._search(driver_daily.content, driver_daily.options, driver_daily.excludes) else: print("题目类型非法") break else: if "填空题" in category: answer = tips driver_weekly.fill_in_blank(answer) elif "多选题" in category: options = driver_weekly.radio_get_options() radio_in_tips, radio_out_tips = "", "" for letter, option in zip(letters, options): for tip in tips: if tip in option: # print(f'{option} in tips') if letter not in radio_in_tips: radio_in_tips += letter radio_out_tips = [ letter for letter, option in zip(letters, options) if (letter not in radio_in_tips) ] print('含 ', radio_in_tips, '不含', radio_out_tips) if len( radio_in_tips ) > 1: # and radio_in_tips not in driver_weekly.excludes: print('根据提示', radio_in_tips) driver_weekly.radio_check(radio_in_tips) elif len(radio_out_tips ) > 1: # and radio_out_tips not in excludes print('根据提示', radio_out_tips) driver_weekly.radio_check(radio_out_tips) # return driver_weekly._search(content, options, excludes) else: print('无法根据提示判断,准备搜索……') elif "单选题" in category: options = driver_weekly.radio_get_options() if '因此本题选' in tips: check = [x for x in letters if x in tips] driver_weekly.radio_check(check) else: radio_in_tips, radio_out_tips = "", "" ''' option_elements = driver_weekly.wait.until(driver_weekly.EC.presence_of_all_elements_located( (driver_weekly.By.XPATH, '//*[@id="app"]/div/div[2]/div/div[4]/div[1]'))) # option_elements = self.find_elements(rules['challenge_options']) options = [x.get_attribute("name") for x in option_elements]''' for letter, option in zip(letters, options): for tip in tips: if tip in option: # print(f'{option} in tips') if letter not in radio_in_tips: radio_in_tips += letter else: # print(f'{option} out tips') if letter not in radio_out_tips: radio_out_tips += letter print('含 ', radio_in_tips, '不含', radio_out_tips) if 1 == len( radio_in_tips ): # and radio_in_tips not in driver_weekly.excludes: print('根据提示', radio_in_tips) driver_weekly.radio_check(radio_in_tips) elif 1 == len( radio_out_tips ): # and radio_out_tips not in excludes print('根据提示', radio_out_tips) driver_weekly.radio_check(radio_out_tips) # return driver_weekly._search(content, options, excludes) else: print('无法根据提示判断,准备搜索……') else: print("题目类型非法") break time.sleep(1) total, scores = show_score(cookies) if scores["weekly"] >= const.weekly_all: print("检测到每周答题分数已满,退出学习") driver_weekly.quit() try: driver_weekly.quit() except Exception as e: print('……') else: print("每周答题之前学完了")
def answer_question(quiz_type, cookies, scores, score_all, quiz_xpath, category_xpath, uid=None, driver_default=None): quiz_zh_CN = {"daily": "每日", "weekly": "每周", "zhuanxiang": "专项"} if (quiz_type not in ["daily", "weekly", "zhuanxiang"]): print("quiz_type 错误。收到的quiz_type:" + quiz_type) exit(0) if uid is None: uid = user.get_userId(cookies) if scores[quiz_type] < score_all: # 还没有满分,需要答题 if driver_default is None: driver_ans = Mydriver(nohead=False) ##### driver_ans = Mydriver(nohead=True) else: driver_ans = driver_default driver_daily = driver_ans driver_weekly = driver_ans driver_zhuanxiang = driver_ans driver_ans.driver.maximize_window() print('请保持窗口最大化\n' * 3) driver_ans.get_url("https://www.xuexi.cn/notFound.html") driver_ans.set_cookies(cookies) pass_count = 0 #最大值,用于nohead模式退出 max_count = 0 if scores[quiz_type] < score_all: letters = list("ABCDEFGHIJKLMN") driver_ans.get_url('https://pc.xuexi.cn/points/my-points.html') while driver_ans.title_is(u"我的积分"): # 页面title为积分则一直循环 time.sleep(1) # 等待页面刷新提示 refresh_buttons = driver_ans.driver.find_elements_by_css_selector( ".ant-modal-wrap .ant-btn:not(.ant-btn-primary)") if len(refresh_buttons) > 0: # refresh_buttons[0].click() driver_ans.click_xpath(quiz_xpath) # 点击各个题目的去答题按钮 time.sleep(1) if quiz_type != "daily": # 如果是每日答题就不用找available了 # 此处修改是因为页面可能刷新后导致的查找元素button 丢失从而引发异常重新此处用可以重新查找来解决 try: to_click = find_available_quiz(quiz_type, driver_ans, uid) except Exception as e: to_click = find_available_quiz(quiz_type, driver_ans, uid) if to_click is not None: to_click.click() time.sleep(0.5) else: print(color.blue("无题可答。即将跳过。")) if driver_default is None: try: driver_ans.quit() except Exception as e: print('driver_ans 在 answer_question 退出时出了一点小问题...') else: pass #其他函数传入函数的driver,不自动退出 while scores[quiz_type] < score_all: try: category = driver_ans.xpath_getText( category_xpath) #获取题目类型 get_attribute("name") except Exception as e: print('查找题目类型...查找元素失败!') break print(category) if quiz_type == "daily": ans_results = driver_ans.driver.find_elements_by_css_selector( ".practice-result .infos .info") if (len(ans_results) != 0): #已经找到答题结果页面 print(ans_results[0].get_attribute("innerHTML")) print(ans_results[0].text) print(ans_results[2].get_attribute("innerHTML")) print(ans_results[2].text) time.sleep(1) # exit(2) break log_daily("\n====================") log_daily(log_timestamp()) log_daily("【" + category + "】") log_daily("【题干】") q_body = driver_ans.driver.find_element_by_css_selector( ".q-body") q_html = q_body.get_attribute('innerHTML') q_text = q_body.text print(q_text) log_daily(q_html) tips, tip_full_text = driver_ans._view_tips() if quiz_type == "daily": log_daily("【提示信息】") log_daily(str(tips) + "\n" + tip_full_text) if not tips: print("本题没有提示") max_count += 1 pass_count += 1 if max_count >= 100 and globalvar.nohead == True: print("略过次数已经超过100次,且出于Nohead模式,退出答题") break if pass_count >= 5: ##### print( "暂时略过已达到 5 次,【 建议您将此题目的题干、提示、选项信息提交到github问题收集issue:https://github.com/TechXueXi/TechXueXi/issues/29 】" ) auto.prompt("等待用户手动答题...完成后请在此按回车...") pass_count = 0 if quiz_type == "daily": ##### log_daily("!!!!!本题没有找到提示,暂时略过!!!!!") auto.prompt("等待用户手动答题...完成后请在此按回车...") time.sleep(1) if "填空题" in category: print('没有找到提示,暂时略过') ##### print('使用默认答案 好 ') #如无填空答案,使用默认答案 好 字 by Sean ##### tips = ['好'] continue ##### elif "多选题" in category: print('没有找到提示,暂时略过') ##### print('使用默认答案 全选') #by Sean continue ##### elif "单选题" in category: print('没有找到提示,暂时略过') # 如无单选答案,使用默认答案 ##### print('使用默认答案 B') #by Sean continue ##### # return driver_daily._search(driver_daily.content, driver_daily.options, driver_daily.excludes) else: print("题目类型非法") if quiz_type == "daily": log_daily("!!!!!无提示,题目类型非法!!!!!") break else: if "填空题" in category: answer = tips if quiz_type != "zhuanxiang": driver_ans.fill_in_blank(answer) else: driver_ans.zhuanxiang_fill_in_blank(answer) elif "多选题" in category: if quiz_type == "daily": options = driver_daily.radio_get_options() ##### len_option = len(options) log_daily("【多选题选项】") log_daily(str(options)) radio_in_tips, radio_out_tips = "", "" for letter, option in zip(letters, options): for tip in tips: if tip in option: # print(f'{option} in tips') if letter not in radio_in_tips: radio_in_tips += letter radio_out_tips = [ letter for letter, option in zip(letters, options) if (letter not in radio_in_tips) ] print('包含提示的选项 ', radio_in_tips, ',不包含提示的选项 ', radio_out_tips) log_daily('包含提示的选项 ' + str(radio_in_tips) + ',不包含提示的选项 ' + str(radio_out_tips)) if len( radio_in_tips ) > 1: # and radio_in_tips not in driver_daily.excludes: print('根据提示', radio_in_tips) driver_daily.radio_check(radio_in_tips) elif len( radio_out_tips ) > 1: # and radio_out_tips not in excludes print('根据提示', radio_out_tips) driver_daily.radio_check(radio_out_tips) # return driver_daily._search(content, options, excludes) else: print('无法根据提示判断,请自行答题……') log_daily("!!!!!无法根据提示判断,请自行答题……!!!!!") ##### print('将使用默认全选答题') #by Sean ##### len_option = len(options) ##### radio_in_tips = letters[:len_option] ##### driver_daily.radio_check(radio_in_tips) auto.prompt("等待用户手动答题...完成后请在此按回车...") elif quiz_type == "weekly": options = driver_weekly.radio_get_options() radio_in_tips, radio_out_tips = "", "" for letter, option in zip(letters, options): for tip in tips: if tip in option: # print(f'{option} in tips') if letter not in radio_in_tips: radio_in_tips += letter radio_out_tips = [ letter for letter, option in zip(letters, options) if (letter not in radio_in_tips) ] print('含 ', radio_in_tips, '不含', radio_out_tips) if len( radio_in_tips ) > 1: # and radio_in_tips not in driver_weekly.excludes: print('根据提示', radio_in_tips) driver_weekly.radio_check(radio_in_tips) elif len( radio_out_tips ) > 1: # and radio_out_tips not in excludes print('根据提示', radio_out_tips) driver_weekly.radio_check(radio_out_tips) # return driver_weekly._search(content, options, excludes) else: print('无法根据提示判断,请自行准备搜索……') ##### print('将使用默认全选答题') #by Sean ##### len_option = len(options) ##### radio_in_tips = letters[:len_option] ##### driver_weekly.radio_check(radio_in_tips) auto.prompt("等待用户手动答题...完成后请在此按回车...") elif quiz_type == "zhuanxiang": options = driver_zhuanxiang.radio_get_options() radio_in_tips, radio_out_tips = "", "" for letter, option in zip(letters, options): for tip in tips: if tip in option: # print(f'{option} in tips') if letter not in radio_in_tips: radio_in_tips += letter radio_out_tips = [ letter for letter, option in zip(letters, options) if (letter not in radio_in_tips) ] print('含 ', radio_in_tips, '不含', radio_out_tips) if len( radio_in_tips ) > 1: # and radio_in_tips not in driver_zhuanxiang.excludes: print('根据提示', radio_in_tips) driver_zhuanxiang.radio_check(radio_in_tips) elif len( radio_out_tips ) > 1: # and radio_out_tips not in excludes print('根据提示', radio_out_tips) driver_zhuanxiang.radio_check(radio_out_tips) # return driver_zhuanxiang._search(content, options, excludes) else: print('无法根据提示判断,请自行准备搜索……') ##### print('将使用默认全选答题') #by Sean ##### len_option = len(options) ##### radio_in_tips = letters[:len_option] ##### driver_zhuanxiang.radio_check(radio_in_tips) auto.prompt("等待用户手动答题...完成后请在此按回车...") elif "单选题" in category: if quiz_type == "daily": options = driver_daily.radio_get_options() log_daily("【单选题选项】") log_daily(str(options)) if '因此本题选' in tips: #提示类型1 check = [x for x in letters if x in tips] log_daily("根据提示类型1,选择答案:" + str(check)) driver_daily.radio_check(check) else: radio_in_tips, radio_out_tips = "", "" ''' option_elements = driver_daily.wait.until(driver_daily.EC.presence_of_all_elements_located( (driver_daily.By.XPATH, '//*[@id="app"]/div/div[2]/div/div[4]/div[1]'))) # option_elements = self.find_elements(rules['challenge_options']) options = [x.get_attribute("name") for x in option_elements]''' for letter, option in zip(letters, options): for tip in tips: if tip in option: # print(f'{option} in tips') if letter not in radio_in_tips: radio_in_tips += letter else: # print(f'{option} out tips') if letter not in radio_out_tips: radio_out_tips += letter print('包含提示的选项 ', radio_in_tips, ',不包含提示的选项 ', radio_out_tips) log_daily('包含提示的选项 ' + str(radio_in_tips) + ',不包含提示的选项 ' + str(radio_out_tips)) if 1 == len( radio_in_tips ): # and radio_in_tips not in driver_daily.excludes: print('根据提示', radio_in_tips) driver_daily.radio_check(radio_in_tips) elif 1 == len( radio_out_tips ): # and radio_out_tips not in excludes print('根据提示', radio_out_tips) driver_daily.radio_check(radio_out_tips) # return driver_daily._search(content, options, excludes) else: print('无法根据提示判断,请自行答题……') log_daily("!!!!!无法根据提示判断,请自行答题……!!!!!") ##### print('将使用默认选 B') #by Sean ##### radio_in_tips = "B" ##### driver_daily.radio_check(radio_in_tips) auto.prompt("等待用户手动答题...完成后请在此按回车...") elif quiz_type == "weekly": options = driver_weekly.radio_get_options() if '因此本题选' in tips: check = [x for x in letters if x in tips] driver_weekly.radio_check(check) else: radio_in_tips, radio_out_tips = "", "" ''' option_elements = driver_weekly.wait.until(driver_weekly.EC.presence_of_all_elements_located( (driver_weekly.By.XPATH, '//*[@id="app"]/div/div[2]/div/div[4]/div[1]'))) # option_elements = self.find_elements(rules['challenge_options']) options = [x.get_attribute("name") for x in option_elements]''' for letter, option in zip(letters, options): for tip in tips: if tip in option: # print(f'{option} in tips') if letter not in radio_in_tips: radio_in_tips += letter else: # print(f'{option} out tips') if letter not in radio_out_tips: radio_out_tips += letter print('含 ', radio_in_tips, '不含', radio_out_tips) if 1 == len( radio_in_tips ): # and radio_in_tips not in driver_weekly.excludes: print('根据提示', radio_in_tips) driver_weekly.radio_check(radio_in_tips) elif 1 == len( radio_out_tips ): # and radio_out_tips not in excludes print('根据提示', radio_out_tips) driver_weekly.radio_check(radio_out_tips) # return driver_weekly._search(content, options, excludes) else: print('无法根据提示判断,请自行准备搜索……') ##### print('将使用默认选 B') #by Sean ##### radio_in_tips = "B" ##### driver_weekly.radio_check(radio_in_tips) auto.prompt("等待用户手动答题...完成后请在此按回车...") elif quiz_type == "zhuanxiang": options = driver_zhuanxiang.radio_get_options() if '因此本题选' in tips: check = [x for x in letters if x in tips] driver_zhuanxiang.radio_check(check) else: radio_in_tips, radio_out_tips = "", "" ''' option_elements = driver_zhuanxiang.wait.until(driver_zhuanxiang.EC.presence_of_all_elements_located( (driver_zhuanxiang.By.XPATH, '//*[@id="app"]/div/div[2]/div/div[4]/div[1]'))) # option_elements = self.find_elements(rules['challenge_options']) options = [x.get_attribute("name") for x in option_elements]''' for letter, option in zip(letters, options): for tip in tips: if tip in option: # print(f'{option} in tips') if letter not in radio_in_tips: radio_in_tips += letter else: # print(f'{option} out tips') if letter not in radio_out_tips: radio_out_tips += letter print('含 ', radio_in_tips, '不含', radio_out_tips) if 1 == len( radio_in_tips ): # and radio_in_tips not in driver_zhuanxiang.excludes: print('根据提示', radio_in_tips) driver_zhuanxiang.radio_check( radio_in_tips) elif 1 == len( radio_out_tips ): # and radio_out_tips not in excludes print('根据提示', radio_out_tips) driver_zhuanxiang.radio_check( radio_out_tips) # return driver_zhuanxiang._search(content, options, excludes) else: print('无法根据提示判断,请自行准备搜索……') ##### print('将使用默认选 B') #by Sean ##### radio_in_tips = "B" ##### driver_zhuanxiang.radio_check(radio_in_tips) auto.prompt("等待用户手动答题...完成后请在此按回车...") else: print("题目类型非法") if quiz_type == "daily": log_daily("!!!!!有提示,但题目类型非法!!!!!") break time.sleep(1) total, scores = show_score(cookies) if scores[quiz_type] >= score_all: print("检测到" + quiz_zh_CN[quiz_type] + "答题分数已满,退出学 xi ") else: print("!!!!!没拿到满分,请收集日志反馈错误题目!!!!!") auto.prompt("完成后(或懒得弄)请在此按回车...") #log_daily("!!!!!没拿到满分!!!!!") if driver_default == None: try: driver_ans.quit() except Exception as e: print('driver_ans 在 answer_question 退出时出了一点小问题...') else: pass #其他函数传入函数的driver,不自动退出 else: print(quiz_zh_CN[quiz_type] + "答题已满分.")
def daily(cookies, scores): if scores["daily"] < const.daily_all: # driver_daily = Mydriver(nohead=nohead) time.sleep(random.randint(5, 15)) driver_daily = Mydriver(nohead=False) driver_daily.driver.maximize_window() print('请保持窗口最大化') print('请保持窗口最大化') print('请保持窗口最大化') driver_daily.get_url("https://www.xuexi.cn/notFound.html") driver_daily.set_cookies(cookies) try_count = 0 if scores["daily"] < const.daily_all: letters = list("ABCDEFGHIJKLMN") driver_daily.get_url('https://pc.xuexi.cn/points/my-points.html') driver_daily.click_xpath( '//*[@id="app"]/div/div[2]/div/div[3]/div[2]/div[5]/div[2]/div[2]/div' ) while scores["daily"] < const.daily_all: try: category = driver_daily.xpath_getText( '//*[@id="app"]/div/div[2]/div/div[4]/div[1]/div[1]' ) # get_attribute("name") except Exception as e: print('查找元素失败!') break print(category) tips = driver_daily._view_tips() check_delay() if not tips: print("本题没有提示") if "填空题" in category: print('没有找到提示,暂时略过') continue elif "多选题" in category: print('没有找到提示,暂时略过') continue elif "单选题" in category: print('没有找到提示,暂时略过') continue # return driver_daily._search(driver_daily.content, driver_daily.options, driver_daily.excludes) else: print("题目类型非法") break else: if "填空题" in category: answer = tips driver_daily.fill_in_blank(answer) elif "多选题" in category: options = driver_daily.radio_get_options() radio_in_tips, radio_out_tips = "", "" for letter, option in zip(letters, options): for tip in tips: if tip in option: # print(f'{option} in tips') if letter not in radio_in_tips: radio_in_tips += letter radio_out_tips = [ letter for letter, option in zip(letters, options) if (letter not in radio_in_tips) ] print('含 ', radio_in_tips, '不含', radio_out_tips) if len( radio_in_tips ) > 1: # and radio_in_tips not in driver_daily.excludes: print('根据提示', radio_in_tips) driver_daily.radio_check(radio_in_tips) elif len(radio_out_tips ) > 1: # and radio_out_tips not in excludes print('根据提示', radio_out_tips) driver_daily.radio_check(radio_out_tips) # return driver_daily._search(content, options, excludes) else: print('无法根据提示判断,准备搜索……') elif "单选题" in category: options = driver_daily.radio_get_options() if '因此本题选' in tips: check = [x for x in letters if x in tips] driver_daily.radio_check(check) else: radio_in_tips, radio_out_tips = "", "" ''' option_elements = driver_daily.wait.until(driver_daily.EC.presence_of_all_elements_located( (driver_daily.By.XPATH, '//*[@id="app"]/div/div[2]/div/div[4]/div[1]'))) # option_elements = self.find_elements(rules['challenge_options']) options = [x.get_attribute("name") for x in option_elements]''' for letter, option in zip(letters, options): for tip in tips: if tip in option: # print(f'{option} in tips') if letter not in radio_in_tips: radio_in_tips += letter else: # print(f'{option} out tips') if letter not in radio_out_tips: radio_out_tips += letter print('含 ', radio_in_tips, '不含', radio_out_tips) if 1 == len( radio_in_tips ): # and radio_in_tips not in driver_daily.excludes: print('根据提示', radio_in_tips) driver_daily.radio_check(radio_in_tips) elif 1 == len( radio_out_tips ): # and radio_out_tips not in excludes print('根据提示', radio_out_tips) driver_daily.radio_check(radio_out_tips) # return driver_daily._search(content, options, excludes) else: print('无法根据提示判断,准备搜索……') else: print("题目类型非法") break time.sleep(1) total, scores = show_score(cookies) if scores["daily"] >= const.daily_all: print("检测到每日答题分数已满,退出学习") driver_daily.quit() try: driver_daily.quit() except Exception as e: print('……') else: print("每日答题之前学完了")
def answer_question(quiz_type, cookies, scores, score_all, quiz_xpath, category_xpath, uid=None, driver_default=None): quiz_zh_CN = {"daily": "每日", "weekly": "每周", "zhuanxiang": "专项"} if (quiz_type not in ["daily", "weekly", "zhuanxiang"]): print("quiz_type 错误。收到的quiz_type:" + quiz_type) exit(0) if uid is None: uid = user.get_userId(cookies) if scores[quiz_type] < score_all: # 还没有满分,需要答题 if driver_default is None: driver_ans = Mydriver(nohead=False) else: driver_ans = driver_default driver_daily = driver_ans driver_weekly = driver_ans driver_zhuanxiang = driver_ans driver_ans.driver.maximize_window() print('请保持窗口最大化\n' * 3) driver_ans.get_url("https://www.xuexi.cn/notFound.html") driver_ans.set_cookies(cookies) try_count = 0 if scores[quiz_type] < score_all: letters = list("ABCDEFGHIJKLMN") driver_ans.get_url('https://pc.xuexi.cn/points/my-points.html') driver_ans.click_xpath(quiz_xpath) # 点击各个题目的去答题按钮 time.sleep(2) if quiz_type != "daily": # 如果是每日答题就不用找available了 to_click = find_available_quiz(quiz_type, driver_ans, uid) if to_click is not None: to_click.click() time.sleep(0.5) else: print(color.blue("无题可答。即将跳过。")) if driver_default == None: try: driver_ans.quit() except Exception as e: print('driver_ans 在 answer_question 退出时出了一点小问题...') else: pass #其他函数传入函数的driver,不自动退出 while scores[quiz_type] < score_all: if quiz_type == "weekly": '''# # ======= # flag = 1 # page_num = 1 # last_page = int(driver_weekly.driver.find_element_by_xpath('//*[@id="app"]/div/div[2]/div/div[5]/ul/li[last()-1]/a').text) # while page_num < last_page and flag == 1: # print('进入每周答题第'+ str(page_num) +'页') # all_month = len(driver_weekly.driver.find_elements_by_class_name('month')) # cur_month = 1 # for tem in range(0, all_month): # for tem2 in range(0, 6): # if flag == 0: # break # try: # temword = driver_weekly.driver.find_element_by_xpath( # '//*[@id="app"]/div/div[2]/div/div[4]/div/div[' + str(tem + 1) + ']/div[2]/div[' + str( # tem2 + 1) + ']/button').text # except: # temword = '' # if all_month == cur_month: # driver_weekly.click_xpath( # '//*[@id="app"]/div/div[2]/div/div[5]/ul/li[' + str(page_num + 2) + ']') # print('切换至下一页') # page_num += 1 # time.sleep(2) # cur_month += 1 # break # name_list = ["开始答题", "继续答题"] # if flag == 1 and (any(name in temword for name in name_list)): # driver_weekly.click_xpath( # '//*[@id="app"]/div/div[2]/div/div[4]/div/div[' + str(tem + 1) + ']/div[2]/div[' + str( # tem2 + 1) + ']/button') # flag = 0 # elif '重新答题' in temword: # continue # while each[6] < 5 and try_count < 10: # >>>>>>> dev # ''' try: category = driver_ans.xpath_getText( #获取题目类型 category_xpath) # get_attribute("name") except Exception as e: print('查找题目类型...查找元素失败!') break print(category) if quiz_type == "daily": ans_results = driver_ans.driver.find_elements_by_css_selector( ".practice-result .infos .info") if (len(ans_results) != 0): #已经找到答题结果页面 print(ans_results[0].get_attribute("innerHTML")) print(ans_results[0].text) print(ans_results[2].get_attribute("innerHTML")) print(ans_results[2].text) time.sleep(3) # exit(2) break log_daily("\n====================") log_daily(log_timestamp()) log_daily("【" + category + "】") log_daily("【题干】") q_body = driver_ans.driver.find_element_by_css_selector( ".q-body") q_html = q_body.get_attribute('innerHTML') q_text = q_body.text print(q_text) log_daily(q_html) tips, tip_full_text = driver_ans._view_tips() if quiz_type == "daily": log_daily("【提示信息】") log_daily(str(tips) + "\n" + tip_full_text) if not tips: print("本题没有提示") if quiz_type == "daily": log_daily("!!!!!本题没有找到提示,暂时略过!!!!!") input("等待用户手动答题...完成后请在此按回车...") time.sleep(3) if "填空题" in category: print('没有找到提示,暂时略过') continue elif "多选题" in category: print('没有找到提示,暂时略过') continue elif "单选题" in category: print('没有找到提示,暂时略过') continue # return driver_daily._search(driver_daily.content, driver_daily.options, driver_daily.excludes) else: print("题目类型非法") if quiz_type == "daily": log_daily("!!!!!无提示,题目类型非法!!!!!") break else: if "填空题" in category: answer = tips if quiz_type != "zhuanxiang": driver_ans.fill_in_blank(answer) else: driver_ans.zhuanxiang_fill_in_blank(answer) elif "多选题" in category: if quiz_type == "daily": options = driver_daily.radio_get_options() log_daily("【多选题选项】") log_daily(str(options)) radio_in_tips, radio_out_tips = "", "" for letter, option in zip(letters, options): for tip in tips: if tip in option: # print(f'{option} in tips') if letter not in radio_in_tips: radio_in_tips += letter radio_out_tips = [ letter for letter, option in zip(letters, options) if (letter not in radio_in_tips) ] print('包含提示的选项 ', radio_in_tips, ',不包含提示的选项 ', radio_out_tips) log_daily('包含提示的选项 ' + str(radio_in_tips) + ',不包含提示的选项 ' + str(radio_out_tips)) if len( radio_in_tips ) > 1: # and radio_in_tips not in driver_daily.excludes: print('根据提示', radio_in_tips) driver_daily.radio_check(radio_in_tips) elif len( radio_out_tips ) > 1: # and radio_out_tips not in excludes print('根据提示', radio_out_tips) driver_daily.radio_check(radio_out_tips) # return driver_daily._search(content, options, excludes) else: print('无法根据提示判断,请自行答题……') log_daily("!!!!!无法根据提示判断,请自行答题……!!!!!") input("等待用户手动答题...完成后请在此按回车...") elif quiz_type == "weekly": options = driver_weekly.radio_get_options() radio_in_tips, radio_out_tips = "", "" for letter, option in zip(letters, options): for tip in tips: if tip in option: # print(f'{option} in tips') if letter not in radio_in_tips: radio_in_tips += letter radio_out_tips = [ letter for letter, option in zip(letters, options) if (letter not in radio_in_tips) ] print('含 ', radio_in_tips, '不含', radio_out_tips) if len( radio_in_tips ) > 1: # and radio_in_tips not in driver_weekly.excludes: print('根据提示', radio_in_tips) driver_weekly.radio_check(radio_in_tips) elif len( radio_out_tips ) > 1: # and radio_out_tips not in excludes print('根据提示', radio_out_tips) driver_weekly.radio_check(radio_out_tips) # return driver_weekly._search(content, options, excludes) else: print('无法根据提示判断,请自行准备搜索……') input("等待用户手动答题...完成后请在此按回车...") elif quiz_type == "zhuanxiang": options = driver_zhuanxiang.radio_get_options() radio_in_tips, radio_out_tips = "", "" for letter, option in zip(letters, options): for tip in tips: if tip in option: # print(f'{option} in tips') if letter not in radio_in_tips: radio_in_tips += letter radio_out_tips = [ letter for letter, option in zip(letters, options) if (letter not in radio_in_tips) ] print('含 ', radio_in_tips, '不含', radio_out_tips) if len( radio_in_tips ) > 1: # and radio_in_tips not in driver_zhuanxiang.excludes: print('根据提示', radio_in_tips) driver_zhuanxiang.radio_check(radio_in_tips) elif len( radio_out_tips ) > 1: # and radio_out_tips not in excludes print('根据提示', radio_out_tips) driver_zhuanxiang.radio_check(radio_out_tips) # return driver_zhuanxiang._search(content, options, excludes) else: print('无法根据提示判断,请自行准备搜索……') input("等待用户手动答题...完成后请在此按回车...") elif "单选题" in category: if quiz_type == "daily": options = driver_daily.radio_get_options() log_daily("【单选题选项】") log_daily(str(options)) if '因此本题选' in tips: #提示类型1 check = [x for x in letters if x in tips] log_daily("根据提示类型1,选择答案:" + str(check)) driver_daily.radio_check(check) else: radio_in_tips, radio_out_tips = "", "" ''' option_elements = driver_daily.wait.until(driver_daily.EC.presence_of_all_elements_located( (driver_daily.By.XPATH, '//*[@id="app"]/div/div[2]/div/div[4]/div[1]'))) # option_elements = self.find_elements(rules['challenge_options']) options = [x.get_attribute("name") for x in option_elements]''' for letter, option in zip(letters, options): for tip in tips: if tip in option: # print(f'{option} in tips') if letter not in radio_in_tips: radio_in_tips += letter else: # print(f'{option} out tips') if letter not in radio_out_tips: radio_out_tips += letter print('包含提示的选项 ', radio_in_tips, ',不包含提示的选项 ', radio_out_tips) log_daily('包含提示的选项 ' + str(radio_in_tips) + ',不包含提示的选项 ' + str(radio_out_tips)) if 1 == len( radio_in_tips ): # and radio_in_tips not in driver_daily.excludes: print('根据提示', radio_in_tips) driver_daily.radio_check(radio_in_tips) elif 1 == len( radio_out_tips ): # and radio_out_tips not in excludes print('根据提示', radio_out_tips) driver_daily.radio_check(radio_out_tips) # return driver_daily._search(content, options, excludes) else: print('无法根据提示判断,请自行答题……') log_daily("!!!!!无法根据提示判断,请自行答题……!!!!!") input("等待用户手动答题...完成后请在此按回车...") elif quiz_type == "weekly": options = driver_weekly.radio_get_options() if '因此本题选' in tips: check = [x for x in letters if x in tips] driver_weekly.radio_check(check) else: radio_in_tips, radio_out_tips = "", "" ''' option_elements = driver_weekly.wait.until(driver_weekly.EC.presence_of_all_elements_located( (driver_weekly.By.XPATH, '//*[@id="app"]/div/div[2]/div/div[4]/div[1]'))) # option_elements = self.find_elements(rules['challenge_options']) options = [x.get_attribute("name") for x in option_elements]''' for letter, option in zip(letters, options): for tip in tips: if tip in option: # print(f'{option} in tips') if letter not in radio_in_tips: radio_in_tips += letter else: # print(f'{option} out tips') if letter not in radio_out_tips: radio_out_tips += letter print('含 ', radio_in_tips, '不含', radio_out_tips) if 1 == len( radio_in_tips ): # and radio_in_tips not in driver_weekly.excludes: print('根据提示', radio_in_tips) driver_weekly.radio_check(radio_in_tips) elif 1 == len( radio_out_tips ): # and radio_out_tips not in excludes print('根据提示', radio_out_tips) driver_weekly.radio_check(radio_out_tips) # return driver_weekly._search(content, options, excludes) else: print('无法根据提示判断,请自行准备搜索……') input("等待用户手动答题...完成后请在此按回车...") elif quiz_type == "zhuanxiang": options = driver_zhuanxiang.radio_get_options() if '因此本题选' in tips: check = [x for x in letters if x in tips] driver_zhuanxiang.radio_check(check) else: radio_in_tips, radio_out_tips = "", "" ''' option_elements = driver_zhuanxiang.wait.until(driver_zhuanxiang.EC.presence_of_all_elements_located( (driver_zhuanxiang.By.XPATH, '//*[@id="app"]/div/div[2]/div/div[4]/div[1]'))) # option_elements = self.find_elements(rules['challenge_options']) options = [x.get_attribute("name") for x in option_elements]''' for letter, option in zip(letters, options): for tip in tips: if tip in option: # print(f'{option} in tips') if letter not in radio_in_tips: radio_in_tips += letter else: # print(f'{option} out tips') if letter not in radio_out_tips: radio_out_tips += letter print('含 ', radio_in_tips, '不含', radio_out_tips) if 1 == len( radio_in_tips ): # and radio_in_tips not in driver_zhuanxiang.excludes: print('根据提示', radio_in_tips) driver_zhuanxiang.radio_check( radio_in_tips) elif 1 == len( radio_out_tips ): # and radio_out_tips not in excludes print('根据提示', radio_out_tips) driver_zhuanxiang.radio_check( radio_out_tips) # return driver_zhuanxiang._search(content, options, excludes) else: print('无法根据提示判断,请自行准备搜索……') input("等待用户手动答题...完成后请在此按回车...") else: print("题目类型非法") if quiz_type == "daily": log_daily("!!!!!有提示,但题目类型非法!!!!!") break time.sleep(1) total, scores = show_score(cookies) if scores[quiz_type] >= score_all: print("检测到" + quiz_zh_CN[quiz_type] + "答题分数已满,退出学习") else: print("!!!!!没拿到满分,请收集日志反馈错误题目!!!!!") input("完成后(或懒得弄)请在此按回车...") #log_daily("!!!!!没拿到满分!!!!!") if driver_default == None: try: driver_ans.quit() except Exception as e: print('driver_ans 在 answer_question 退出时出了一点小问题...') else: pass #其他函数传入函数的driver,不自动退出 else: print(quiz_zh_CN[quiz_type] + "答题已满分.")
def video(userId, cookies, video_pointer, scores): try: if scores["video_num"] < const.video_num_all or scores[ "video_time"] < const.video_time_all: # driver_video = Mydriver(nohead=nohead) driver_video = Mydriver(nohead=True) # def video_stop_function(): # driver_video.quit() # threads.regist_stop_function(video_stop_function) driver_video.get_url("https://www.xuexi.cn/notFound.html") driver_video.set_cookies(cookies) links = get_links.get_video_links() try_count = 0 watchvideo_time = 0 while True: if scores["video_num"] < const.video_num_all and try_count < 10: v_num = const.video_num_all - scores["video_num"] for i in range(video_pointer, video_pointer + v_num): driver_video.get_url(links[i]) watchvideo_time = 60 + random.randint(5, 15) for j in range(watchvideo_time): if random.random() > 0.5: driver_video.go_js( 'window.scrollTo(0, document.body.scrollHeight/180*{})' .format(j)) print("\r视频数量学习中,视频剩余{}个,本次剩余时间{}秒".format( video_pointer + v_num - i, watchvideo_time - j), end="") time.sleep(1) driver_video.go_js( 'window.scrollTo(0, document.body.scrollHeight)') total, scores = show_score(cookies) if scores["video_num"] >= const.video_num_all: print("检测到视频数量分数已满,退出学习") break video_pointer += v_num else: user.save_video_index(userId, video_pointer) break try_count = 0 while True: if scores[ "video_time"] < const.video_time_all and try_count < 10: num_time = 60 driver_video.get_url(links[video_pointer - 1]) remaining = (const.video_time_all - scores["video_time"]) * 1 * num_time for i in range(remaining): if random.random() > 0.5: driver_video.go_js( 'window.scrollTo(0, document.body.scrollHeight/{}*{})' .format(remaining, i)) print("\r视频时长学习中,视频总时长剩余{}秒".format(remaining - i), end="") time.sleep(1) if i % (60) == 0 and i != remaining: total, scores = show_score(cookies) if scores["video_time"] >= const.video_time_all: print("检测到视频时长分数已满,退出学习") break driver_video.go_js( 'window.scrollTo(0, document.body.scrollHeight)') total, scores = show_score(cookies) else: break if try_count < 10: print("视频学习完成") else: print("视频学习出现异常,请检查 user/article_video_index.json 文件记录") driver_video.quit() else: print("视频之前学完了") except Exception as e: print(color.red("视频学习检测到异常:" + str(e)))
def article(userId, cookies, article_pointer, scores): try: if scores["article_num"] < const.article_num_all or scores[ "article_time"] < const.article_time_all: # driver_article = Mydriver(nohead=nohead) driver_article = Mydriver(nohead=True) # def article_stop_function(): # driver_article.quit() # threads.regist_stop_function(article_stop_function) driver_article.get_url("https://www.xuexi.cn/notFound.html") driver_article.set_cookies(cookies) links = get_links.get_article_links() try_count = 0 readarticle_time = 0 while True: if scores[ "article_num"] < const.article_num_all and try_count < 10: article_remain = const.article_num_all - scores[ "article_num"] for i in range(article_pointer, article_pointer + article_remain): driver_article.get_url(links[i]) readarticle_time = 60 + random.randint(5, 15) for j in range(readarticle_time): if random.random() > 0.5: driver_article.go_js( 'window.scrollTo(0, document.body.scrollHeight/120*{})' .format(j)) print("\r文章数量学习中,文章剩余{}篇,本篇剩余时间{}秒".format( article_pointer + article_remain - i, readarticle_time - j), end="") time.sleep(1) driver_article.go_js( 'window.scrollTo(0, document.body.scrollHeight)') total, scores = show_score(cookies) if scores["article_num"] >= const.article_num_all: print("检测到文章数量分数已满,退出学习") break article_pointer += article_remain else: user.save_article_index(userId, article_pointer) break try_count = 0 while True: if scores[ "article_time"] < const.article_time_all and try_count < 10: num_time = 60 driver_article.get_url(links[article_pointer - 1]) remaining = (const.article_time_all - scores["article_time"]) * 1 * num_time for i in range(remaining): if random.random() > 0.5: driver_article.go_js( 'window.scrollTo(0, document.body.scrollHeight/{}*{})' .format(remaining, i)) print("\r文章时长学习中,文章总时长剩余{}秒".format(remaining - i), end="") time.sleep(1) if i % (60) == 0 and i != remaining: total, scores = show_score(cookies) if scores["article_time"] >= const.article_time_all: print("检测到文章时长分数已满,退出学习") break driver_article.go_js( 'window.scrollTo(0, document.body.scrollHeight)') total, scores = show_score(cookies) else: break if try_count < 10: print("文章学习完成") else: print("文章学习出现异常,请检查 user/article_video_index.json 文件记录") driver_article.quit() else: print("文章之前学完了") except Exception as e: print(color.red("文章学习检测到异常:" + str(e)))