代碼:
1 import time 2 from io import BytesIO 3 from PIL import Image 4 from selenium import webdriver 5 from selenium.webdriver import ActionChains 6 from selenium.webdriver.common.by import By 7 from selenium.webdriver.support.ui import WebDriverWait 8 from selenium.webdriver.support import expected_conditions as EC 9 10 EMAIL = '' # 郵箱 密碼需要自己注冊 11 PASSWORD = '' 12 BORDER = 6 13 INIT_LEFT = 60 14 15 16 class CrackGeetest(): 17 def __init__(self): 18 self.url = 'https://account.geetest.com/login' 19 self.browser = webdriver.Chrome() 20 self.wait = WebDriverWait(self.browser, 20) 21 self.email = EMAIL 22 self.password = PASSWORD 23 24 def __del__(self): 25 self.browser.close() 26 27 def get_geetest_button(self): 28 """ 29 獲取初始驗證按鈕 30 :return: 31 """ 32 button = self.wait.until(EC.element_to_be_clickable((By.CLASS_NAME, 'geetest_radar_tip'))) 33 return button 34 35 def get_position(self): 36 """ 37 獲取驗證碼位置 38 :return: 驗證碼位置元組 39 """ 40 img = self.wait.until(EC.presence_of_element_located((By.CLASS_NAME, 'geetest_canvas_img'))) 41 time.sleep(2) 42 location = img.location 43 size = img.size 44 top, bottom, left, right = location['y'], location['y'] + size['height'], location['x'], location['x'] + size[ 45 'width'] 46 return (top, bottom, left, right) 47 48 def get_screenshot(self): 49 """ 50 獲取網頁截圖 51 :return: 截圖對象 52 """ 53 screenshot = self.browser.get_screenshot_as_png() 54 screenshot = Image.open(BytesIO(screenshot)) 55 return screenshot 56 57 def get_slider(self): 58 """ 59 獲取滑塊 60 :return: 滑塊對象 61 """ 62 slider = self.wait.until(EC.element_to_be_clickable((By.CLASS_NAME, 'geetest_slider_button'))) 63 return slider 64 65 def get_geetest_image(self, name='captcha.png'): 66 """ 67 獲取驗證碼圖片 68 :return: 圖片對象 69 """ 70 top, bottom, left, right = self.get_position() 71 print('驗證碼位置', top, bottom, left, right) 72 screenshot = self.get_screenshot() 73 captcha = screenshot.crop((left, top, right, bottom)) 74 captcha.save(name) 75 return captcha 76 77 def open(self): 78 """ 79 打開網頁輸入用戶名密碼 80 :return: None 81 """ 82 self.browser.get(self.url) 83 email = self.wait.until(EC.presence_of_element_located((By.ID, 'email'))) 84 password = self.wait.until(EC.presence_of_element_located((By.ID, 'password'))) 85 email.send_keys(self.email) 86 password.send_keys(self.password) 87 88 def get_gap(self, image1, image2): 89 """ 90 獲取缺口偏移量 91 :param image1: 不帶缺口圖片 92 :param image2: 帶缺口圖片 93 :return: 94 """ 95 left = 60 96 for i in range(left, image1.size[0]): 97 for j in range(image1.size[1]): 98 if not self.is_pixel_equal(image1, image2, i, j): 99 left = i 100 return left 101 return left 102 103 def is_pixel_equal(self, image1, image2, x, y): 104 """ 105 判斷兩個像素是否相同 106 :param image1: 圖片1 107 :param image2: 圖片2 108 :param x: 位置x 109 :param y: 位置y 110 :return: 像素是否相同 111 """ 112 # 取兩個圖片的像素點 113 pixel1 = image1.load()[x, y] 114 pixel2 = image2.load()[x, y] 115 threshold = 60 116 if abs(pixel1[0] - pixel2[0]) < threshold and abs(pixel1[1] - pixel2[1]) < threshold and abs( 117 pixel1[2] - pixel2[2]) < threshold: 118 return True 119 else: 120 return False 121 122 def get_track(self, distance): 123 """ 124 根據偏移量獲取移動軌跡 125 :param distance: 偏移量 126 :return: 移動軌跡 127 """ 128 # 移動軌跡 129 track = [] 130 # 當前位移 131 current = 0 132 # 減速閾值 133 mid = distance * 4 / 5 134 # 計算間隔 135 t = 0.2 136 # 初速度 137 v = 0 138 139 while current < distance: 140 if current < mid: 141 # 加速度為正2 142 a = 2 143 else: 144 # 加速度為負3 145 a = -3 146 # 初速度v0 147 v0 = v 148 # 當前速度v = v0 + at 149 v = v0 + a * t 150 # 移動距離x = v0t + 1/2 * a * t^2 151 move = v0 * t + 1 / 2 * a * t * t 152 # 當前位移 153 current += move 154 # 加入軌跡 155 track.append(round(move)) 156 return track 157 158 def move_to_gap(self, slider, track): 159 """ 160 拖動滑塊到缺口處 161 :param slider: 滑塊 162 :param track: 軌跡 163 :return: 164 """ 165 ActionChains(self.browser).click_and_hold(slider).perform() 166 for x in track: 167 ActionChains(self.browser).move_by_offset(xoffset=x, yoffset=0).perform() 168 time.sleep(0.5) 169 ActionChains(self.browser).release().perform() 170 171 def login(self): 172 """ 173 登錄 174 :return: None 175 """ 176 submit = self.wait.until(EC.element_to_be_clickable((By.CLASS_NAME, 'login-btn'))) 177 submit.click() 178 time.sleep(10) 179 print('登錄成功') 180 181 def crack(self): 182 # 輸入用戶名密碼 183 self.open() 184 # 點擊驗證按鈕 185 button = self.get_geetest_button() 186 button.click() 187 # 獲取驗證碼圖片 188 image1 = self.get_geetest_image('captcha1.png') 189 # 點按呼出缺口 190 slider = self.get_slider() 191 slider.click() 192 # 獲取帶缺口的驗證碼圖片 193 image2 = self.get_geetest_image('captcha2.png') 194 # 獲取缺口位置 195 gap = self.get_gap(image1, image2) 196 print('缺口位置', gap) 197 # 減去缺口位移 198 gap -= BORDER 199 # 獲取移動軌跡 200 track = self.get_track(gap) 201 print('滑動軌跡', track) 202 # 拖動滑塊 203 self.move_to_gap(slider, track) 204 205 success = self.wait.until( 206 EC.text_to_be_present_in_element((By.CLASS_NAME, 'geetest_success_radar_tip_content'), '驗證成功')) 207 print(success) 208 209 # 失敗后重試 210 if not success: 211 self.crack() 212 else: 213 self.login() 214 215 216 if __name__ == '__main__': 217 crack = CrackGeetest() 218 crack.crack()
