1. 程式人生 > >極客驗證碼破解

極客驗證碼破解

apt else with open 加速度 自動化 position 自動 button inb

很多網站的登陸都有驗證碼一項,而極客的方案就是應用的非常普遍。更多的場景是反反爬蟲的對抗中,極客驗證碼更是首選。

圖片來看一下

技術分享圖片

點擊後就出現上面的滑動圖片的窗口。本文親自嘗試代碼,來分享其中的坑。

本文是使用selenium自動化測試工具來驅動代碼完成驗證,因此要有谷歌的driver和谷歌瀏覽器。以http://www.epicc.com.cn/idprovider/views/login.jsp為例子。

首先

    def get_geetest_button(self):
        """
        獲取初始驗證按鈕
        :return:
        """
        button 
= self.wait.until(EC.element_to_be_clickable((By.CLASS_NAME, slide-tip))) return button def get_position(self): """ 獲取驗證碼位置 :return: 驗證碼位置元組 """ img=self.wait.until(EC.presence_of_element_located((By.CLASS_NAME ,captcha-box-content))) time.sleep(
2) location = img.location print(location) size = img.size top, bottom, left, right = location[y], location[y] + size[height], location[x], location[x] + size[ width] print((top, bottom, left, right)) # return (top, bottom, left, right)
return (238, 400, 1103, 1473) def get_screenshot(self): """ 獲取網頁截圖 :return: 截圖對象 """ name = int(time.time()) screenshot = self.browser.get_screenshot_as_png() with open("screenshot"+ str(name) + ".png","wb")as f: f.write(screenshot) screenshot = Image.open(BytesIO(screenshot)) return screenshot def get_slider(self): """ 獲取滑塊 :return: 滑塊對象 """ slider = self.wait.until(EC.element_to_be_clickable((By.CLASS_NAME, slide-bar))) return slider def get_geetest_image(self, name=captcha.png): """ 獲取驗證碼圖片 :return: 圖片對象 """ top, bottom, left, right = self.get_position() print(驗證碼位置, top, bottom, left, right) time.sleep(3) screenshot = self.get_screenshot() # crop函數帶的參數為(起始點的橫坐標,起始點的縱坐標,寬度,高度) captcha = screenshot.crop((left, top, right, bottom)) captcha.save(name) return captcha def open(self): """ 打開網頁輸入用戶名密碼 :return: None """ self.browser.get(self.url) time.sleep(1) passLoginButton = self.browser.find_element_by_xpath("//ul[@class=‘login-tab-list‘]/li[2]") passLoginButton.click() email = self.wait.until(EC.presence_of_element_located((By.ID, entryId))) password = self.wait.until(EC.presence_of_element_located((By.ID, password))) email.send_keys(self.email) password.send_keys(self.password) def get_gap(self, image1, image2): """ 獲取缺口偏移量 :param image1: 不帶缺口圖片 :param image2: 帶缺口圖片 :return: """ for i in range(INIT_LEFT, image1.size[0]): for j in range(image1.size[1]): if not self.is_pixel_equal(image1, image2, i, j): left = i return left def is_pixel_equal(self, image1, image2, x, y): """ 判斷兩個像素是否相同 :param image1: 圖片1 :param image2: 圖片2 :param x: 位置x :param y: 位置y :return: 像素是否相同 """ # 取兩個圖片的像素點 pixel1 = image1.load()[x, y] pixel2 = image2.load()[x, y] threshold = 60 if abs(pixel1[0] - pixel2[0]) < threshold and abs(pixel1[1] - pixel2[1]) < threshold and abs( pixel1[2] - pixel2[2]) < threshold: return True else: return False def get_track(self, distance): """ 根據偏移量獲取移動軌跡 :param distance: 偏移量 :return: 移動軌跡 """ # 移動軌跡 track = [] # 當前位移 current = 0 # 減速閾值 mid = distance * 4 / 5 # 計算間隔 t = 0.2 # 初速度 v = 0 while current < distance: if current < mid: # 加速度為正2 a = 2 else: # 加速度為負3 a = -3 # 初速度v0 v0 = v # 當前速度v = v0 + at v = v0 + a * t # 移動距離x = v0t + 1/2 * a * t^2 move = v0 * t + 1 / 2 * a * t * t # 當前位移 current += move # 加入軌跡 track.append(round(move)) return track def move_to_gap(self, slider, track): """ 拖動滑塊到缺口處 :param slider: 滑塊 :param track: 軌跡 :return: """ ActionChains(self.browser).click_and_hold(slider).perform() for x in track: ActionChains(self.browser).move_by_offset(xoffset=x, yoffset=0).perform() time.sleep(0.5) ActionChains(self.browser).release().perform() def login(self): """ 登錄 :return: None """ submit = self.wait.until(EC.element_to_be_clickable((By.CLASS_NAME, epiccLogin))) submit.click() time.sleep(10) print(登錄成功) def crack(self): # 輸入用戶名密碼 self.open() # 點擊驗證按鈕 button = self.get_geetest_button() ActionChains(self.browser).move_to_element(button).perform() # button.click() time.sleep(5) # 獲取驗證碼圖片 image1 = self.get_geetest_image(captcha1.png) # 點按呼出缺口 print("點按呼出缺口") slider = self.get_slider() slider.click() # 獲取帶缺口的驗證碼圖片 image2 = self.get_geetest_image(captcha2.png) # 獲取缺口位置 gap = self.get_gap(image1, image2) print(缺口位置, gap) # 減去缺口位移 BORDER指的是待拼合的滑塊距離驗證碼圖片左邊的距離(經過驗證大多數網站滑塊驗證碼這個值是是固定的) gap -= BORDER #滑塊要滑動的距離 print("需要滑動的距離:{}".format(gap)) gap = gap * 0.8 # 獲取移動軌跡 track = self.get_track(gap) print(滑動軌跡, track) # 拖動滑塊 self.move_to_gap(slider, track) success = self.wait.until( EC.text_to_be_present_in_element((By.CLASS_NAME, geetest_success_radar_tip_content), 驗證成功)) print(success) # 失敗後重試 if not success: self.crack() else: self.login() if __name__ == __main__: crack = CrackGeetest() crack.crack()

極客驗證碼破解