class CrakTouClick(): def __init__(self): self.url = 'http://admin.touclick.com/login.html' self.browser = webdriver.Chrome() self.wait = WebDriverWait(self.browser,20) self.email = EMAIL self.password = PASSWORD self.chaojiying = Chaojiying(CHAOJIYING_USERNAME,CHAOJIYING_PASSWORD,CHAOJIYING_SOFT_ID) def open(self): """ 打开网页输入用户民密码 :return: """ self.browser.get(self.url) email = self.wait.until(EC.presence_of_element_located((By.ID,'email'))) password = self.wait.until(EC.presence_of_element_located((By.ID,'password'))) email.send_keys(self.email) password.send_keys(self.password) def get_touclick_button(self): """ 获取初始验证按钮 :return: """ button = self.wait.until(EC.element_to_be_clickable((By.CLASS_NAME,'touclick-hod-wrap'))) return button def get_touclick_element(self): """ 获取验证图片对象 :return: 图片对象 """ element = self.wait.until(EC.presence_of_element_located((By.CLASS_NAME,'touclick-pub-content'))) return element def get_position(self): """ 获取验证码位置 :return: 验证码位置元祖 """ element = self.get_touclick_element() time.sleep(2) location = element.location size = element.size top,bottom,left,right = location['y'],location['y'] + size['height'],location['x'],location['x'] + size['width'] return (top,bottom,left,right) def get_screenshot(self): """ 获取网页截图 :return: """ screenshot = self.browser.get_screenshot_as_png() screenshot = Image.open(BytesIO(screenshot)) return screenshot def get_touclick_image(self,name='captcha.png'): """ 获取验证码图片 :param name: :return: 图片对象 """ top,bottom,left,right = self.get_position() print('验证码位置',top,bottom,left,right) screenshot = self.get_screenshot() captcha = screenshot.crop((left,top,right,bottom)) def get_points(self,captcha_result): """ 解析识别结果 :param captcha_result:识别结果 :return: 转化后的结果 """ groups = captcha_result.get('pic_str').split('|') locations = [[int(number) for number in group.split(',')] for group in groups] return locations def touch_click_words(self,locations): """ 点击验证图片 :param locations:点击位置 :return: """ for location in locations: print(location) ActionChains(self.browser).move_to_element_with_offset(self.get_touclick_element(),location[0], location[1]).click().perform() time.sleep(1) def touch_click_verify(self): """ 点击验证码按钮 :return: """ button = self.wait.until(EC.element_to_be_clickable((By.CLASS_NAME,'touclick-pub-submit'))) button.click() def login(self): """ 登录 :return: """ submit = self.wait.until(EC.element_to_be_clickable((By.ID,'_submit'))) submit.click() time.sleep(10) print('登录成功') def crack(self): """ 破解入口 :return: """ self.open() button = self.get_touclick_button() button.click() image = self.get_touclick_image() bytes_array = BytesIO() image.save(bytes_array,format='PNG') result = self.chaojiying.PostPic(bytes_array.getvalue(),CHAOJIYING_KIND) print(result) locations = self.get_points(result) self.touch_click_words(locations) self.touch_click_verify() success = self.wait.until(EC.text_to_be_present_in_element((By.CLASS_NAME,'touclick-hod-note'),'验证成功')) print(success) if not success: self.crack() else: self.login()
class Login_Cnki(object): def __init__(self, code=None): """ 初始化注册信息 :param code: 验证码 """ self.url = URL # 目标站点 self.browser = webdriver.Chrome() # 声明浏览器对象 self.browser.get(self.url) # 访问网页 self.wait = WebDriverWait(self.browser, 5) # 显式等待5秒,待节点元素全部加载 self.username = USERNAME # 用户名 self.password = PASSWORD # 密码 self.email = EMAIL # 邮箱 self.code = code # 图形验证码 self.chaojiying = Chaojiying(CHAIJIYING_USERNAME, CHAOJIYING_PASSWORD, CHAIJIYING_SOFT_ID) # 创建超级鹰对象 def __del__(self): self.browser.close() def get_position(self): """ 获取验证码位置 :return: 验证码位置元组 """ img = self.wait.until( EC.presence_of_element_located((By.ID, 'checkcode'))) # 获取图形验证码的节点 time.sleep(2) location = img.location # 获取图形验证码在网页中的相对位置 size = img.size # 获取图形验证码的大小 top, bottom, left, right = location[ 'y'], location['y'] + size['height'], location[ 'x'], location['x'] + size['width'] # 分别获取左上角和右下角的坐标 return [top, bottom, left, right] # 返回坐标 def get_screenshot(self): """ 获取网页截图 :return: 截图对象 """ screenshot = self.browser.get_screenshot_as_png() # 获取网页的PNG格式截图 screenshot = Image.open(BytesIO(screenshot)) # 创建Image对象 return screenshot def get_geetest_image(self, name='code.png'): """ 获取验证码图片 :return: 图片对象 """ top, bottom, left, right = self.get_position() # 获取坐标点 print('验证码位置:', top, bottom, left, right) screenshot = self.get_screenshot() # 获取网页截图 captcha = screenshot.crop((left, top, right, bottom)) # 将二维码从网页截图中裁剪下来 captcha.save(name) # 存储为'code.png' return captcha # 返回Image对象 def get(self): """ 获取注册信息输入框节点 :return: """ # 若"立即注册"按钮加载完毕,认为各节点加载完毕. try: self.wait.until( EC.presence_of_element_located((By.ID, 'ButtonRegister'))) except TimeoutException: pass input_username = self.browser.find_element_by_id( 'username') # 获取用户名输入框 input_password = self.browser.find_element_by_id( 'txtPassword') # 获取密码输入框 input_email = self.browser.find_element_by_id('txtEmail') # 获取邮箱输入框 input_code = self.browser.find_element_by_id( 'txtOldCheckCode') # 获取验证码输入框 register = self.browser.find_element_by_id('ButtonRegister') # 获取注册按钮 return [ input_username, input_password, input_email, input_code, register ] def get_result(self, image): """ 获取超级鹰返回的结果 :param image: 验证码的Image对象 :return: 识别结果 """ bytes_array = BytesIO() # 创建一个BytesIO类型的字节数组 image.save(bytes_array, format='PNG') # 写入验证码的二进制流 # 识别验证码 captcha_result = self.chaojiying.PostPic(bytes_array.getvalue(), CHAOJIYING_KIND) # 发送至超级鹰识别验证 result = captcha_result.get('pic_str') # 获取识别结果 return result def login(self): """ 注册登录知网 :return: """ print('正在获取图形验证码...') # _, res_2 = handle_code(self.get_geetest_image()) # 返回结果为两个,可任意选取一种结果(你认为识别精度比较高的那一个) # self.code = _ # 赋值任意一个code self.code = self.get_result(self.get_geetest_image()) username, password, email, code, register = self.get() # 获取节点 username.send_keys(self.username) # 输入用户名 time.sleep(2) password.send_keys(self.password) # 输入密码 time.sleep(2) email.send_keys(self.email) # 输入邮箱 time.sleep(2) code.send_keys(self.code) # 输入验证码 time.sleep(2) register.click() # 注册 time.sleep(3) print('注册成功!')
def crack(): # 保存网页截图 browser.save_screenshot('222.png') # 获取 验证码确定按钮 button = browser.find_element_by_xpath( xpath='//div[@class="geetest_panel"]/a/div') # 获取 验证码图片的 位置信息 img1 = browser.find_element_by_xpath( xpath='//div[@class="geetest_widget"]') location = img1.location size = img1.size top, bottom, left, right = location['y'], location['y'] + size[ 'height'], location['x'], location['x'] + size['width'] print('图片的宽:', img1.size['width']) print(top, bottom, left, right) # 根据获取的验证码位置信息和网页图片 对验证码图片进行裁剪 保存 img_1 = Image.open('222.png') capcha1 = img_1.crop((left, top, right, bottom - 54)) capcha1.save('tu1-1.png') # 接入超级鹰 API 获取图片中的一些参数 (返回的是一个字典) cjy = Chaojiying('zhoufang', '000000', '898324') im = open('tu1-1.png', 'rb').read() content = cjy.PostPic(im, 9004) print(content) # 将图片中汉字的坐标位置 提取出来 positions = content.get('pic_str').split('|') locations = [[int(number) for number in group.split(",")] for group in positions] print(positions) print(locations) # 根据获取的坐标信息 模仿鼠标点击验证码图片 for location1 in locations: print(location1) ActionChains(browser).move_to_element_with_offset( img1, location1[0], location1[1]).click().perform() time.sleep(2) button.click() time.sleep(2) # 失败后重试 lower = browser.find_element_by_xpath( '//div[@class="geetest_table_box"]/div[2]').text print('判断', lower) if '验证成功' in lower: print('登录成功') time.sleep(3) # if lower != '验证失败 请按提示重新操作' and lower != None: # print('登录成功') # time.sleep(3) else: time.sleep(3) print('登录失败') # 登录失败后 , 调用 该函数 , 后台 则对该次判断不做扣分处理 pic_id = content.get('pic_id') print('图片id为:', pic_id) cjy = Chaojiying('zhoufang', '000000', '898324') cjy.ReportError(pic_id) crack()
class Login_Jianshu(object): def __init__(self): """ 初始化 """ self.url = URL self.driver = webdriver.Chrome() # 声明浏览器对象 self.wait = WebDriverWait(self.driver, 5) # 设置显式等待5秒 self.user_phone = USER_PHONENUMBER # 简书账号 self.password = PASSWORD # 密码 self.chaojiying = Chaojiying(CHAIJIYING_USERNAME, CHAOJIYING_PASSWORD, CHAIJIYING_SOFT_ID) # 创建超级鹰对象 def __del__(self): self.driver.close() def open(self): """ 打开简书网页版登录界面输入账号和密码 :return: None """ self.driver.get(URL) account = self.wait.until( EC.presence_of_element_located( (By.ID, 'session_email_or_mobile_number'))) password = self.wait.until( EC.presence_of_element_located((By.ID, 'session_password'))) account.send_keys(self.user_phone) sleep(3) password.send_keys(self.password) def get_submit_button(self): """ 获取登录按钮 :return: 登录按钮 """ button = self.wait.until( EC.presence_of_element_located((By.CLASS_NAME, 'sign-in-button'))) return button def get_screenshot(self): """ 获取网页截图 :return: 截图对象 """ screenshot = self.driver.get_screenshot_as_png() screenshot = Image.open(BytesIO(screenshot)) return screenshot def get_verifi_element(self): """ 获取验证码 :return: 验证码节点 """ element = self.wait.until( EC.presence_of_element_located( (By.CLASS_NAME, 'geetest_item_img'))) return element def get_position(self): """ 获取验证码位置 :return: 验证码位置列表 """ element = self.get_verifi_element() sleep(2) location = element.location size = element.size top, bottom, left, right = location['y'], location['y'] + size[ 'height'], location['x'], location['x'] + size['width'] return [left, top, right, bottom] # 返回验证图片的两个坐标值 def get_verifi_image(self, name='verifi.png'): """ 获取验证码图片 :return: 图片对象 """ left, top, right, bottom = self.get_position() print('验证码位置:', left, top, right, bottom) screenshot = self.get_screenshot() # 获取网页截图的Image对象 jianshu_code = screenshot.crop((left, top, right, bottom)) # 裁剪验证码 jianshu_code.save(name) # 存储照片 return jianshu_code def get_points(self, verified_result): """ 获取超级鹰的识别结果 :param verified_result: 识别结果 :return: 转化后的结果 """ # 获取结果 groups = verified_result.get('pic_str').split('|') # 将字符串转化为整形 locations = [[int(number) for number in group.split(',')] for group in groups] return locations def touch_click(self, locations): """ 点击点触式验证码 :param locations: 点击位置 :return: None """ for location in locations: print(location) ActionChains(self.driver).move_to_element_with_offset( self.get_verifi_element(), location[0], location[1]).click().perform() sleep(1) def verifi_button(self): """ 确认按钮 :return: None """ submit = self.wait.until( EC.presence_of_element_located( (By.CLASS_NAME, 'geetest_commit_tip'))) submit.click() def crack(self): """ 破解 """ # 打开简书登录界面 self.open() # 点击登录按钮 button = self.get_submit_button() button.click() # 获取验证码图片 image = self.get_verifi_image() bytes_array = BytesIO() image.save(bytes_array, format='PNG') # 识别验证码 result = self.chaojiying.PostPic(bytes_array.getvalue(), CHAOJIYING_KIND) print("超级鹰识别结果:", result) locations = self.get_points(result) self.touch_click(locations) sleep(3) # 点击确认按钮 self.verifi_button() # 通过获取"用户头像节点"判断是否登录成功 sleep(8) success = self.wait.until( EC.presence_of_element_located((By.CLASS_NAME, 'avatar'))) print('已获取到头像节点!') # 失败重试 if not success: print("-" * 50) self.crack() else: print('登录成功!') sleep(5)