class CrackTouClick(): def __init__(self): self.url = 'https://fishc.com.cn/forum-173-1.html' self.option = ChromeOptions() self.option.add_experimental_option('excludeSwitches', ['enable-automation']) self.browser = Chrome(options=self.option) 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: None """ self.browser.get(self.url) email = self.wait.until( EC.presence_of_element_located((By.ID, 'ls_username'))) password = self.wait.until( EC.presence_of_element_located((By.ID, 'ls_password'))) email.send_keys(self.email) password.send_keys(self.password) button = self.wait.until( EC.element_to_be_clickable((By.CLASS_NAME, 'pn'))).click() try: slider = self.wait.until( EC.element_to_be_clickable( (By.CLASS_NAME, 'td-pop-slidetrigger'))) CHAOJIYING_KIND = 9202 print('这是滑块验证码') image = self.get_touclick_image() bytes_array = BytesIO() image.save(bytes_array, format='PNG') # 识别验证码 result = self.chaojiying.post_pic(bytes_array.getvalue(), CHAOJIYING_KIND) print('验证码位置', result['pic_str']) print(result) locations = self.get_points(result) self.touch_click_words(locations) print('正在检测错误,此处延迟3秒,以便等待页面加载') time.sleep(3) self.img_error(result) except Exception as e: print('这是字体验证码', e) CHAOJIYING_KIND = 9103 # 获取验证码图片 image = self.get_touclick_image() bytes_array = BytesIO() image.save(bytes_array, format='PNG') # 识别验证码 result = self.chaojiying.post_pic(bytes_array.getvalue(), CHAOJIYING_KIND) print(result) locations = self.get_points(result) self.touch_click_words2(locations) print('正在检测错误,此处延迟3秒,以便等待页面加载') time.sleep(3) self.img_error(result) def touch_click_verify(self): """ 获取滑块按钮 :return: None """ slider = self.wait.until( EC.element_to_be_clickable((By.CLASS_NAME, 'td-pop-slidetrigger'))) return slider def get_touclick_element(self): """ 获取验证图片对象 :return: 图片对象 """ element = self.wait.until( EC.presence_of_element_located((By.CLASS_NAME, 'td-bg-img'))) 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'): """ 获取验证码图片 :return: 图片对象 """ top, bottom, left, right = self.get_position() screenshot = self.get_screenshot() captcha = screenshot.crop((left, top, right, bottom)) captcha.save(name) return captcha 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: None """ for location in locations: print(location) ActionChains(self.browser).drag_and_drop_by_offset( self.touch_click_verify(), location[0], location[1]).perform() time.sleep(1) def touch_click_words2(self, locations): """ 点击字体验证图片 :param locations: 点击位置 :return: None """ 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 img_error(self, result): #检测验证码有没有出错,这步老是报出栈错误 #无奈之下只能采取解析式来判断登录前后页面的数据了 #不得不吐槽的是,这里的滑块验证码坐标识别率低到了令人发指的地步 test = etree.HTML(self.browser.page_source) title = test.xpath('//*[@id="um"]/p[1]/strong/a/@title') print('爬取登陆前后的数据变化', title) if title == []: img_id = result['pic_id'] self.chaojiying.report_error(img_id) print('登录失败,已发送错误验证码') self.open() else: print('登录成功')
class CrackWeibo(): def __init__(self, username, password): self.url = 'https://passport.weibo.cn/signin/login' self.browser = webdriver.Chrome(chrome_options=chrome_options) self.wait = WebDriverWait(self.browser, 20) self.phone = username self.password = password self.chaojiying = Chaojiying(CHAOJIYING_USERNAME, CHAOJIYING_PASSWORD, CHAOJIYING_SOFT_ID) def open(self): """打开网页,输入用户名和密码,点击登陆,切换到验证码的页面""" self.browser.get(self.url) phone = self.wait.until(EC.visibility_of_element_located((By.XPATH, '//*[@id="loginName"]'))) password = self.wait.until(EC.visibility_of_element_located((By.ID, 'loginPassword'))) phone.send_keys(self.phone) password.send_keys(self.password) button = self.wait.until(EC.visibility_of_element_located((By.XPATH, '//*[@id="loginAction"]'))) button.click() def password_error(self): """ 判断是否密码错误 :return: """ try: return WebDriverWait(self.browser, 5).until( EC.text_to_be_present_in_element((By.ID, 'errorMsg'), '用户名或密码错误')) except TimeoutException: return False def login_successfully(self): """ 判断是否登录成功 :return: """ try: return bool( WebDriverWait(self.browser, 5).until(EC.presence_of_element_located((By.XPATH, '//*[@id="app"]/div[1]/div[1]/div[1]/div[1]')))) except TimeoutException: return False def get_weibo_button(self): """获取验证按钮""" button = self.wait.until(EC.visibility_of_element_located((By.XPATH, '//*[@id="embed-captcha"]/div/div[2]/div[1' ']/div[3]/span[1]'))) return button def get_weibo_element(self): """获取验证图片对象""" element = self.wait.until(EC.visibility_of_element_located((By.XPATH, '/html/body/div[3]/div[2]/div[1]'))) return element def get_position(self, element): """获取验证图片对象位置""" 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_weibo_image(self, top, bottom, left, right): """获取验证码图片""" screenshot = self.browser.get_screenshot_as_png() screenshot = Image.open(BytesIO(screenshot)).resize((1200, 764)) captcha = screenshot.crop((left, top+30, right, bottom+30)) return captcha def picture_recognition(self, image): """图片的文字位置,用超级鹰识别,返回识别的json格式""" bytes_array = BytesIO() image.save(bytes_array, format='PNG') result = self.chaojiying.post_pic(bytes_array.getvalue(), CHAOJIYING_KIND) return result def get_word_points(self, captcha_result): """解析文字的识别结果""" 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): """点击验证码图片里的文字""" for location in locations: ActionChains(self.browser).move_to_element_with_offset(self.get_weibo_element(), location[0], location[1]-30).click().perform() time.sleep(1) button = self.wait.until(EC.visibility_of_element_located((By.XPATH, '/html/body/div[3]/div[2]/div[1]/div/div/d' 'iv[3]/a/div'))) button.click() def validation_error(self, result): """调用超级鹰识别失败接口返回积分""" im_id = result['pic_id'] self.chaojiying.report_error(im_id) def get_cookies(self): """获取登陆后的页面信息,然后关闭浏览器""" return self.browser.get_cookies() self.browser.close() def main(self): """破解入口""" SUCCESS = False # 登陆页面的操作,跳转到验证页面 self.open() if self.password_error(): return { 'status': 2, 'content': '用户名或密码错误' } else: # 验证页面的操作 self.get_weibo_button().click() # 点击验证按钮 # 如果不需要验证码直接登录成功 if self.login_successfully(): cookies = self.get_cookies() return { 'status': 1, 'content': cookies } else: while not SUCCESS: print("开始识别验证码") element = self.get_weibo_element() # 获取验证码对象 print("开始获取元素位置") top, bottom, left, right = self.get_position(element) # 获取元素的位置 print("根据元素位置",top, bottom, left, right, '获取验证码图片') image = self.get_weibo_image(top, bottom, left, right) # 获取验证码图片 print("超级鹰开始识别图片") captcha_result = self.picture_recognition(image) # 传图片给超级鹰接口识别,返回json格式的识别结果 print("识别结果:", captcha_result) locations = self.get_word_points(captcha_result) # 解析超级鹰的识别结果 print("解析识别结果为坐标参数:", locations, "\n根据坐标开始点击验证码图片") # 根据识别结果,点击验证码 self.touch_click_words(locations) #判断是否验证成功 SUCCESS = self.login_successfully() if SUCCESS: cookies = self.get_cookies() return { 'status': 3, 'content': cookies } else: self.validation_error(captcha_result) print("识别失败,重新开始\n")
class Crack12306: def __init__(self): self._url = 'https://kyfw.12306.cn/otn/login/init' self._browser = webdriver.Chrome() self._wait = WebDriverWait(self._browser, 20) self._username = USERNAME self._password = PASSWORD self._chaojiying = Chaojiying(CHAOJIYING_USERNAME, CHAOJIYING_PASSWORD, CHAOJIYING_SOFT_ID) self._pic_id = None def __del__(self): self._browser.close() def open(self): """ 打开网页输入用户名密码 :return: None """ self._browser.get(self._url) username = self._wait.until( EC.presence_of_element_located((By.ID, 'username'))) password = self._wait.until( EC.presence_of_element_located((By.ID, 'password'))) username.send_keys(self._username) password.send_keys(self._password) def get_12306_element(self): """ 获取验证图片对象 :return: 图片对象 """ element = self._wait.until( EC.presence_of_element_located( (By.CSS_SELECTOR, '.touclick .touclick-image'))) return element def get_position(self): """ 获取验证码位置 :return: 验证码位置元组 """ element = self.get_12306_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_screen_shot(self): """ 获取网页截图 :return: 截图对象 """ screen_shot = self._browser.get_screenshot_as_png() screen_shot = Image.open(BytesIO(screen_shot)) return screen_shot def get_12306_image(self, name='captcha.png'): """ 获取验证码图片 :return: 图片对象 """ top, bottom, left, right = self.get_position() print('验证码位置', top, bottom, left, right) screen_shot = self.get_screen_shot() captcha = screen_shot.crop((left, top, right, bottom)) captcha.save(name) return captcha 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: None """ element = self.get_12306_element() for location in locations: print(location) ActionChains(self._browser).move_to_element_with_offset( element, location[0], location[1]).click().perform() time.sleep(1) def login(self): """ 登录 :return: None """ submit = self._wait.until( EC.element_to_be_clickable((By.ID, 'loginSub'))) submit.click() time.sleep(10) def check_login_success(self): """ 检查是否登录成功 :return: None """ success = False try: self._browser.find_element_by_id('loginSub') self._chaojiying.report_error(self._pic_id) except NoSuchElementException: success = True return success def crack(self): """ 破解入口 :return: None """ self.open() # 获取验证码图片 image = self.get_12306_image() bytes_array = BytesIO() image.save(bytes_array, format='PNG') # 识别验证码 result = self._chaojiying.post_pic(bytes_array.getvalue(), CHAOJIYING_KIND) self._pic_id = result.get('pic_id') print(result) locations = self.get_points(result) self.touch_click_words(locations) # 登录 self.login() # 失败后重试 success = self.check_login_success() if not success: self.crack() else: print('登录成功')
class CrackTouClick(): def __init__(self): self.url = 'https://kyfw.12306.cn/otn/login/init' 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 __del__(self): self.browser.close() def open(self): """ 打开网页输入用户名密码 :return: None """ self.browser.get(self.url) email = self.wait.until( EC.presence_of_element_located((By.ID, 'username'))) 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'))) 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'): """ 获取验证码图片 :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) return captcha 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: None """ 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: None """ button = self.wait.until( EC.element_to_be_clickable((By.CLASS_NAME, 'touclick-pub-submit'))) button.click() def login(self): """ 登录 :return: None """ submit = self.wait.until( EC.element_to_be_clickable((By.ID, 'loginSub'))) submit.click() time.sleep(10) print('登录成功') def crack(self): """ 破解入口 :return: None """ 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.post_pic(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) time.sleep(2) self.login() # 失败后重试 try: success = self.wait.until( EC.text_to_be_present_in_element((By.ID, 'login_user'), '用户名')) print(success) print('success') except TimeoutException: # 如果失败了超级鹰会返回分值 self.chaojiying.report_error(result['pic_id']) self.crack()
class Crack12306(): def __init__(self): self.url = 'https://kyfw.12306.cn/otn/login/init' self.browser = webdriver.Chrome() # self.browser.set_window_size(1920, 1080) self.wait = WebDriverWait(self.browser, 20) self.username = USERNAME self.password = PASSWORD self.chaojiying = Chaojiying(CHAOJIYING_USERNAME, CHAOJIYING_PASSWORD, CHAOJIYING_SOFT_ID) def __del__(self): print('正在关闭浏览器') time.sleep(10) self.browser.close() def open(self): """ 打开网页输入用户名密码 :return: None """ self.browser.get(self.url) username = self.wait.until( EC.presence_of_element_located((By.ID, 'username'))) password = self.wait.until( EC.presence_of_element_located((By.ID, 'password'))) username.send_keys(self.username) password.send_keys(self.password) def get_12306_element(self): """ 获取验证图片对象 :return: 图片对象 """ element = self.wait.until( EC.presence_of_element_located( (By.CSS_SELECTOR, '.touclick .touclick-image'))) return element def get_position(self): """ 获取验证码位置 :return: 验证码位置元组 """ element = self.get_12306_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_12306_image(self, name='captcha.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) return captcha def get_screenshot(self): """ 获取网页截图 :return: 截图对象 """ screenshot = self.browser.get_screenshot_as_png() screenshot = Image.open(BytesIO(screenshot)) return screenshot def get_points(self, captcha_result): """ 解析识别结果 :param captcha_result: 识别结果 :return: 转化后的结果 """ # get_points()将识别结果变成列表的形式。 # 超级鹰识别后返回结果:{'err_no': 0, 'err_str': 'OK', 'pic_id': '9082718423155200001', 'pic_str': '55,152|176,148', 'md5': '508d3222a99aa1a5218ccbfcda3a2857'} groups = captcha_result.get('pic_str').split('|') print(groups) # locations = [[55, 152],[176, 148]] locations = [[int(number) for number in group.split(',')] for group in groups] return locations def touch_click_words(self, locations): """ 点击验证图片 :param locations: 点击位置 :return: None """ # touch_click_words()方法则通过调用move_to_element_with_offset()方法依次传入解析后的坐标,点击即可。 element = self.get_12306_element() for location in locations: print(location) ActionChains(self.browser).move_to_element_with_offset( element, location[0], location[1]).click().perform() time.sleep(1) def login(self): """ 登录 :return: None """ submit = self.wait.until( EC.element_to_be_clickable((By.ID, 'loginSub'))) submit.click() time.sleep(10) def check_login_success(self): """ 检查是否登录成功 :return: None """ success = False try: # 登录成功后的页面是找不到loginSub这个元素的。 self.browser.find_element_by_id('loginSub') self.chaojiying.report_error(self.pic_id) except NoSuchElementException: success = True return success def crack(self): """ 破解入口 :return: None """ self.open() # 获取验证码图片 image = self.get_12306_image() bytes_array = BytesIO() image.save(bytes_array, format='PNG') # 识别验证码 result = self.chaojiying.post_pic(bytes_array.getvalue(), CHAOJIYING_KIND) self.pic_id = result.get('pic_id') print(result) locations = self.get_points(result) self.touch_click_words(locations) # 登录 self.login() # 失败后重试 success = self.check_login_success() if not success: self.crack() else: print('登录成功')