def get_cracked_string_by_xpath(self, xpath): self.logger.debug(f"start to capture capcha image") CAPTCHA_IMG_PATH = f"capcha_data/capcha_{datetime.now().strftime('%m-%d %H:%M:%S')}.png" Path(CAPTCHA_IMG_PATH).parent.mkdir(parents=True, exist_ok=True) captcha_img_element = self.driver.find_element_by_xpath(xpath) with open(CAPTCHA_IMG_PATH, "wb") as f: f.write(captcha_img_element.screenshot_as_png) self.logger.debug(f"start to convert capcha to string") captcha_str = captcha.captcha_to_string(Image.open(CAPTCHA_IMG_PATH)) self.logger.debug(f"captcha convert result: {captcha_str}") return captcha_str
def fill_captcha(browser, wait): res_text = "" correct_res = "https://www.1point3acres.com/bbs/static/image/common/check_right.gif" wrong_res = "https://www.1point3acres.com/bbs/static/image/common/check_error.gif" cap_input_element = wait.until(ec.visibility_of_element_located((By.XPATH, "//input[@name='seccodeverify']"))) trial = 1 while res_text == "" or res_text == wrong_res: # 验证码解码错误 print(f"开始破解图形验证码,第{trial}次尝试...") # 重新获取验证码 get_new_captcha = wait.until(ec.visibility_of_element_located((By.XPATH, "//a[text()='换一个']"))) get_new_captcha.click() captcha_img_element = wait.until(ec.visibility_of_element_located((By.XPATH, "//span[text()='输入下图中的字符']//img"))) sleep(1) captcha_img_element = browser.find_element_by_xpath("//span[text()='输入下图中的字符']//img") loc = captcha_img_element.location size = captcha_img_element.size left, right = loc['x'], loc['x'] + size['width'] top, bottom = loc['y'], loc['y'] + size['height'] browser.save_screenshot('screenshot.png') scrsht = Image.open("screenshot.png") captcha_img = scrsht.crop((left, top, right, bottom)) captcha_img.save("captcha.png") # 解码验证码,转化为字符串 captcha_text = captcha.captcha_to_string(Image.open("captcha.png")) print(f"图形验证码破解结果: {captcha_text}") cap_input_element.send_keys(captcha_text) # 选择答案以激活正确或错误图标 answer_element = browser.find_element_by_xpath("//input[@name='answer'][@value='1']") answer_element.click() # 等待错误或正确图标出现,为的是检验刚才输入的验证码是否正确 wait.until( ec.presence_of_element_located((By.CSS_SELECTOR, "img[src='static/image/common/check_right.gif'], img[src='static/image/common/check_error.gif']")) ) check_image_element = browser.find_element_by_xpath("//span[@id='checkseccodeverify_SA00']//img") res_text = check_image_element.get_attribute("src") if res_text == correct_res: print("验证码输入正确 ^_^ ") else: print("验证码输入错误!") trial += 1
def solver(path): return captcha_to_string(Image.open(path))
def fill_captcha(browser, wait): res_text = "" correct_res = "https://www.1point3acres.com/bbs/static/image/common/check_right.gif" wrong_res = "https://www.1point3acres.com/bbs/static/image/common/check_error.gif" sleep(4) # cap_input_element = wait.until(ec.visibility_of_element_located((By.XPATH, "//input[@name='seccodeverify']"))) cap_input_element = browser.find_element_by_xpath("//input[@name='seccodeverify']") trial = 1 while res_text == "" or res_text != correct_res: # 验证码解码错误 if trial >= 20: return logger.debug(f"开始破解图形验证码,第{trial}次尝试...") # 重新获取验证码 sleep(3) # get_new_captcha = wait.until(ec.visibility_of_element_located((By.XPATH, "//a[text()='换一个']"))) get_new_captcha = browser.find_element_by_xpath("//a[text()='换一个']") browser.execute_script("arguments[0].click();", get_new_captcha) sleep(3) # captcha_img_element = wait.until(ec.visibility_of_element_located((By.XPATH, "//span[text()='输入下图中的字符']//img"))) captcha_img_element = browser.find_element_by_xpath("//span[text()='输入下图中的字符']//img") # src = captcha_img_element.get_attribute("src") browser.save_screenshot('screenshot.png') scrsht = Image.open("screenshot.png") # * capture img # loc = captcha_img_element.location # size = captcha_img_element.size # left, right = loc['x'], loc['x'] + size['width'] # top, bottom = loc['y'], loc['y'] + size['height'] # captcha_img = scrsht.crop((left, top, right, bottom)) # captcha_img.save("captcha.png") # * test captcha_img = cap_input_element.screenshot_as_png with open ("captcha.png", "wb") as f: f.write(captcha_img_element.screenshot_as_png) # * image -> captcha captcha_text = captcha.captcha_to_string(Image.open("captcha.png")) logger.debug(f"图形验证码破解结果: {captcha_text}") cap_input_element.send_keys(captcha_text) # 选择答案以激活正确或错误图标 answer_element = browser.find_element_by_xpath("//input[@name='answer'][@value='1']") answer_element.click() # 等待错误或正确图标出现,为的是检验刚才输入的验证码是否正确 # wait.until(ec.presence_of_element_located((By.CSS_SELECTOR, "img[src='static/image/common/check_right.gif'], img[src='static/image/common/check_error.gif']")) ) sleep(4) check_image_element = browser.find_element_by_xpath("//span[@id='checkseccodeverify_SA00']//img") res_text = check_image_element.get_attribute("src") print(res_text) if res_text == correct_res: logger.debug("验证码输入正确 ^_^ ") else: logger.debug("验证码输入错误!") trial += 1
def crack_capcha(img_path: str) -> str: logger.debug(f"[crack] start: {img_path}") captcha_str = captcha.captcha_to_string(Image.open(img_path)) logger.debug(f"[crack] result: {captcha_str}") return captcha_str