def swipe_left(self): # 向右滑动屏幕 self.driver.swipe( self.size['width'] * SecureRandom.uniform(0.89, 0.98), self.size['height'] * SecureRandom.uniform(0.75, 0.89), self.size['width'] * SecureRandom.uniform(0.01, 0.11), self.size['height'] * SecureRandom.uniform(0.75, 0.89), SecureRandom.uniform(800, 1200)) logger.debug('向左滑动屏幕')
def swipe_down(self): # 向下滑动屏幕 self.driver.swipe( self.size['width'] * SecureRandom.uniform(0.55, 0.65), self.size['height'] * SecureRandom.uniform(0.25, 0.35), self.size['width'] * SecureRandom.uniform(0.55, 0.65), self.size['height'] * SecureRandom.uniform(0.65, 0.75), SecureRandom.uniform(800, 1200)) logger.debug('向下滑动屏幕')
def _search(self, content, options, exclude=''): logger.debug(f'搜索 {content} <exclude = {exclude}>') logger.info(f"选项 {options}") content = re.sub(r'[\((]出题单位.*', "", content) if options[-1].startswith("以上") and chr(len(options) + 64) not in exclude: logger.info(f'根据经验: {chr(len(options)+64)} 很可能是正确答案') return chr(len(options) + 64) # url = quote('https://www.baidu.com/s?wd=' + content, safe=string.printable) url = quote("https://www.sogou.com/web?query=" + content, safe=string.printable) response = requests.get(url, headers=self.headers).text counts = [] for i, option in zip(['A', 'B', 'C', 'D', 'E', 'F'], options): count = response.count(option) counts.append((count, i)) logger.info(f'{i}. {option}: {count} 次') counts = sorted(counts, key=lambda x: x[0], reverse=True) counts = [x for x in counts if x[1] not in exclude] c, i = counts[0] # 降序排列第一个为计数最大值 if 0 == c: # 替换了百度引擎为搜狗引擎,结果全为零的机会应该会大幅降低 _, i = SecureRandom.choice(counts) logger.info(f'搜索结果全0,随机一个 {i}') logger.info(f'根据搜索结果: {i} 很可能是正确答案') return i
def _challenge(self): logger.info(f'挑战答题 目标 {self.challenge_count} 题, Go!') while True: result = self._challenge_cycle(self.challenge_count) if 0 >= result: logger.info(f'已成功挑战 {self.challenge_count} 题,正在返回') break else: delay_time = SecureRandom.randint(5, 10) logger.info( f'本次挑战 {self.challenge_count - result} 题,{delay_time} 秒后再来一组' ) time.sleep(delay_time) continue
def _challenge_init(self): # super().__init__() try: self.challenge_count = cfg.getint('prefers', 'challenge_count') except: g, t = self.score["挑战答题"] if t == g: self.challenge_count = 0 else: self.challenge_count = SecureRandom.randint( cfg.getint('prefers', 'challenge_count_min'), cfg.getint('prefers', 'challenge_count_max')) self.delay_bot = cfg.getint('prefers', 'challenge_delay_min') self.delay_top = cfg.getint('prefers', 'challenge_delay_max')
def _challenge_cycle(self, num): self.safe_click(rules['challenge_entry']) while num: content = self.wait.until( EC.presence_of_element_located( (By.XPATH, rules['challenge_content']))).get_attribute("name") option_elements = self.wait.until( EC.presence_of_all_elements_located( (By.XPATH, rules['challenge_options']))) options = [x.get_attribute("name") for x in option_elements] length_of_options = len(options) logger.info(f'<{num}> {content}') answer = self._verify(category='单选题', content=content, options=options) delay_time = SecureRandom.randint(self.delay_bot, self.delay_top) logger.info(f'随机延时 {delay_time} 秒提交答案: {answer}') time.sleep(delay_time) option_elements[ord(answer) - 65].click() try: time.sleep(2) wrong = self.driver.find_element_by_xpath( rules['challenge_revival']) logger.debug(f'很遗憾回答错误') self._update_bank({ "category": "单选题", "content": content, "options": options, "answer": "", "excludes": answer, "notes": "" }) break except: logger.debug(f'回答正确') num -= 1 self._update_bank({ "category": "单选题", "content": content, "options": options, "answer": answer, "excludes": "", "notes": "" }) else: logger.info(f'已完成指定题量, 本题故意答错后自动退出,否则延时30秒等待死亡') content = self.wait.until( EC.presence_of_element_located( (By.XPATH, rules['challenge_content']))).get_attribute("name") option_elements = self.wait.until( EC.presence_of_all_elements_located( (By.XPATH, rules['challenge_options']))) options = [x.get_attribute("name") for x in option_elements] length_of_options = len(options) logger.info(f'<{num}> {content}') answer = self._verify(category='单选题', content=content, options=options) final_choose = ( (ord(answer) - 65) + SecureRandom.randint(1, length_of_options)) % length_of_options delay_time = SecureRandom.randint(self.delay_bot, self.delay_top) logger.info(f'随机延时 {delay_time} 秒提交答案: {chr(final_choose+65)}') time.sleep(delay_time) option_elements[final_choose].click() time.sleep(2) try: wrong = self.driver.find_element_by_xpath( rules['challenge_revival']) logger.debug(f'恭喜回答错误') except: logger.debug('抱歉回答正确') time.sleep(30) self.safe_back('challenge -> quiz') return num