def add_user(chat_id=None): get_argv() gl.pushprint("请登录(登录方式请仔细阅读文档,如果觉得这是让你下载,就是你没仔细读文档):", chat_id=chat_id) driver_login = Mydriver() cookies = driver_login.login(chat_id) driver_login.quit() if not cookies: gl.pushprint("登录超时。", chat_id=chat_id) return user.save_cookies(cookies) uid = user.get_userId(cookies) user_fullname = user.get_fullname(uid) user.update_last_user(uid) gl.pushprint(user_fullname + "登录成功", chat_id=chat_id)
def start_learn(uid, name): nohead, lock, stime = get_argv() print("是否无头模式:{0} {1}".format(nohead, os.getenv('Nohead'))) cookies = user.get_cookie(uid) if nohead == True: TechXueXi_mode = "3" else: try: if cfg["base"]["ModeType"]: print("默认选择模式:" + str(cfg["base"]["ModeType"]) + "\n" + "=" * 60) TechXueXi_mode = str(cfg["base"]["ModeType"]) except Exception as e: TechXueXi_mode = "3" if not name: user_fullname = user.get_fullname(uid) else: user_fullname = uid + "_" + name if not cookies or TechXueXi_mode == "0": msg = "" if name == "新用户": msg = "需要增加新用户,请扫码登录,否则请无视" else: msg = user_fullname + "登录信息失效,请重新扫码" print(msg) gl.pushprint(msg) driver_login = Mydriver() cookies = driver_login.login() driver_login.quit() if not cookies: print("登录超时") return user.save_cookies(cookies) uid = user.get_userId(cookies) user_fullname = user.get_fullname(uid) user.update_last_user(uid) output = "\n用户:" + user_fullname + "登录正常,开始学习...\n" article_index = user.get_article_index(uid) video_index = 1 # user.get_video_index(uid) total, scores = show_score(cookies) gl.pushprint(output) if TechXueXi_mode in ["1", "3"]: article_thread = threads.MyThread("文章学 xi ", article, uid, cookies, article_index, scores, lock=lock) video_thread = threads.MyThread("视频学 xi ", video, uid, cookies, video_index, scores, lock=lock) article_thread.start() video_thread.start() article_thread.join() video_thread.join() if TechXueXi_mode in ["2", "3"]: driver_default = Mydriver() print('开始每日答题……') daily(cookies, scores, driver_default=driver_default) if TechXueXi_mode in ["2", "3"]: print('开始每周答题……') weekly(cookies, scores, driver_default=driver_default) if nohead != True: print('开始专项答题……') zhuanxiang(cookies, scores, driver_default=driver_default) try: driver_default.quit() except Exception as e: gl.pushprint('driver_default 在 main 退出时出了一点小问题...') if TechXueXi_mode == "4": user.select_user() if TechXueXi_mode == "5": user.refresh_all_cookies(display_score=True) if TechXueXi_mode == "6": user.refresh_all_cookies(live_time=11.90) seconds_used = int(time.time() - start_time) print("总计用时 " + str(math.floor(seconds_used / 60)) + " 分 " + str(seconds_used % 60) + " 秒") show_scorePush(cookies) try: user.shutdown(stime) except Exception as e: pass
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 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 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 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("每日答题之前学完了")
user.list_user() # user.select_user() print("=" * 60, '''\nTechXueXi 现支持以下模式(答题时请值守电脑旁处理少部分不正常的题目):''') print(cfg['base']['ModeText'] + '\n' + "=" * 60) # 模式提示文字请在 ./config/main.ini 处修改。 # TechXueXi_mode = input("请选择模式(输入对应数字)并回车: ") # 懒人之最版 TechXueXi_mode = cfg['base']['Mode'] info_shread = threads.MyThread("获取更新信息...", version.up_info) info_shread.start() # 1 创建用户标记,区分多个用户历史纪录 uid = user.get_default_userId() if not cookies: print("未找到有效登录信息,需要登录") driver_login = Mydriver(nohead=False) cookies = driver_login.login() user.save_cookies(cookies) uid = user.get_userId(cookies) user_fullname = user.get_fullname(uid) user.update_last_user(uid) try: driver_login.quit() except: print("wtf:退出webdriver失败?") pass article_index = user.get_article_index(uid) video_index = user.get_video_index(uid) total, scores = show_score(cookies) nohead, lock, stime = get_argv()
try: if cfg["base"]["ModeType"]: print("默认选择模式:" + str(cfg["base"]["ModeType"]) + "\n" + "=" * 60) TechXueXi_mode = str(cfg["base"]["ModeType"]) except Exception as e: TechXueXi_mode = input("请选择模式(输入对应数字)并回车: ") info_shread = threads.MyThread("获取更新信息...", version.up_info) info_shread.start() # 1 创建用户标记,区分多个用户历史纪录 uid = user.get_default_userId() if not cookies or TechXueXi_mode == "0": print("未找到有效登录信息,需要登录") driver_login = Mydriver() cookies = driver_login.login() driver_login.quit() user.save_cookies(cookies) uid = user.get_userId(cookies) user_fullname = user.get_fullname(uid) user.update_last_user(uid) # 增加多用户支持,已经有登录信息的重新扫码 else: user_fullname = user.get_fullname(uid) output = "\n用户" + user_fullname + "已登录,如要再次学习请重新扫码\n" print(output) gl.pushprint(output) driver_login = Mydriver() cookies = driver_login.login() driver_login.quit()
"=" * 60) # 模式提示文字请在 ./config/main.ini 处修改。 try: if cfg["base"]["ModeType"]: print("默认选择模式:" + cfg["base"]["ModeType"] + "\n" + "=" * 60) TechXueXi_mode = cfg["base"]["ModeType"] except Exception as e: TechXueXi_mode = input("请选择模式(输入对应数字)并回车: ") info_shread = threads.MyThread("获取更新信息...", version.up_info) info_shread.start() # 1 创建用户标记,区分多个用户历史纪录 uid = user.get_default_userId() if not cookies: print("未找到有效登录信息,需要登录") driver_login = Mydriver(nohead=False) cookies = driver_login.login() driver_login.quit() user.save_cookies(cookies) uid = user.get_userId(cookies) user_fullname = user.get_fullname(uid) user.update_last_user(uid) article_index = user.get_article_index(uid) video_index = user.get_video_index(uid) total, scores = show_score(cookies) nohead, lock, stime = get_argv() if TechXueXi_mode in ["1", "2", "3"]: article_thread = threads.MyThread("文章学习", article,
def start_learn(uid, name): # 0 读取版本信息 start_time = time.time() nohead, lock, stime, Single = get_argv() print("是否无头模式:{0} {1}".format(nohead, os.getenv('Nohead'))) cookies = user.get_cookie(uid) if nohead == True: TechXueXi_mode = "3" else: TechXueXi_mode = str(cfg_get("base.ModeType", 3)) print("当前选择模式:" + TechXueXi_mode + "\n" + "=" * 60) if not name: user_fullname = user.get_fullname(uid) name = user_fullname.split('_', 1)[1] else: user_fullname = uid + "_" + name if not cookies or TechXueXi_mode == "0": msg = "" if name == "新用户": msg = "需要增加新用户,请扫码登录,否则请无视" else: msg = name + " 登录信息失效,请重新扫码" # print(msg) gl.pushprint(msg, chat_id=uid) if gl.pushmode == "6": gl.pushprint("web模式跳过自动获取二维码,请手动点击添加按钮", chat_id=uid) print(color.red("【#️⃣】 若直接退出请运行:webserverListener.py")) return driver_login = Mydriver() cookies = driver_login.login() driver_login.quit() if not cookies: print("登录超时") return user.save_cookies(cookies) uid = user.get_userId(cookies) user_fullname = user.get_fullname(uid) name = user_fullname.split('_', 1)[1] user.update_last_user(uid) output = name + " 登录正常,开始学习...\n" article_index = user.get_article_index(uid) video_index = 1 # user.get_video_index(uid) total, scores = show_score(cookies) gl.pushprint(output, chat_id=uid) if TechXueXi_mode in ["1", "3"]: article_thread = threads.MyThread("文章学 xi ", article, uid, cookies, article_index, scores, lock=lock) video_thread = threads.MyThread("视频学 xi ", video, uid, cookies, video_index, scores, lock=lock) article_thread.start() video_thread.start() article_thread.join() video_thread.join() if TechXueXi_mode in ["2", "3"]: print('开始每日答题……') daily(cookies, scores) print('开始每周答题……') weekly(cookies, scores) if nohead != True or gl.zhuanxiang == True: print('开始专项答题……') zhuanxiang(cookies, scores) if TechXueXi_mode == "4": user.select_user() if TechXueXi_mode == "5": user.refresh_all_cookies(display_score=True) if TechXueXi_mode == "6": user.refresh_all_cookies(live_time=11.90) seconds_used = int(time.time() - start_time) gl.pushprint(name + " 总计用时 " + str(math.floor(seconds_used / 60)) + " 分 " + str(seconds_used % 60) + " 秒", chat_id=uid) show_scorePush(cookies, chat_id=uid) try: user.shutdown(stime) except Exception as e: pass
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)))