Kernel_wu 快速學習的實踐者 python3 破解 geetest(極驗)的滑塊驗證碼 from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.action_chains import ActionChains import PIL.Image as image import time,re, random import requests try: from StringIO import StringIO except ImportError: from io import StringIO #爬蟲模擬的瀏覽器頭部信息 agent = 'Mozilla/5.0 (Windows NT 5.1; rv:33.0) Gecko/20100101 Firefox/33.0' headers = { 'User-Agent': agent } # 根據位置對圖片進行合並還原 # filename:圖片 # location_list:圖片位置 #內部兩個圖片處理函數的介紹 #crop函數帶的參數為(起始點的橫坐標,起始點的縱坐標,寬度,高度) #paste函數的參數為(需要修改的圖片,粘貼的起始點的橫坐標,粘貼的起始點的縱坐標) def get_merge_image(filename,location_list): #打開圖片文件 im = image.open(filename) #創建新的圖片,大小為260*116 new_im = image.new('RGB', (260,116)) im_list_upper=[] im_list_down=[] # 拷貝圖片 for location in location_list: #上面的圖片 if location['y']==-58: im_list_upper.append(im.crop((abs(location['x']),58,abs(location['x'])+10,166))) #下面的圖片 if location['y']==0: im_list_down.append(im.crop((abs(location['x']),0,abs(location['x'])+10,58))) new_im = image.new('RGB', (260,116)) x_offset = 0 #黏貼圖片 for im in im_list_upper: new_im.paste(im, (x_offset,0)) x_offset += im.size[0] x_offset = 0 for im in im_list_down: new_im.paste(im, (x_offset,58)) x_offset += im.size[0] return new_im #下載並還原圖片 # driver:webdriver # div:圖片的div def get_image(driver,div): #找到圖片所在的div background_images=driver.find_elements_by_xpath(div) location_list=[] imageurl='' #圖片是被CSS按照位移的方式打亂的,我們需要找出這些位移,為后續還原做好准備 for background_image in background_images: location={} #在html里面解析出小圖片的url地址,還有長高的數值 location['x']=int(re.findall("background-image: url\(\"(.*)\"\); background-position: (.*)px (.*)px;",background_image.get_attribute('style'))[0][1]) location['y']=int(re.findall("background-image: url\(\"(.*)\"\); background-position: (.*)px (.*)px;",background_image.get_attribute('style'))[0][2]) imageurl=re.findall("background-image: url\(\"(.*)\"\); background-position: (.*)px (.*)px;",background_image.get_attribute('style'))[0][0] location_list.append(location) #替換圖片的后綴,獲得圖片的URL imageurl=imageurl.replace("webp","jpg") #獲得圖片的名字 imageName = imageurl.split('/')[-1] #獲得圖片 session = requests.session() r = session.get(imageurl, headers = headers, verify = False) #下載圖片 with open(imageName, 'wb') as f: f.write(r.content) f.close() #重新合並還原圖片 image=get_merge_image(imageName, location_list) return image #對比RGB值 def is_similar(image1,image2,x,y): pass #獲取指定位置的RGB值 pixel1=image1.getpixel((x,y)) pixel2=image2.getpixel((x,y)) for i in range(0,3): # 如果相差超過50則就認為找到了缺口的位置 if abs(pixel1[i]-pixel2[i])>=50: return False return True #計算缺口的位置 def get_diff_location(image1,image2): i=0 # 兩張原始圖的大小都是相同的260*116 # 那就通過兩個for循環依次對比每個像素點的RGB值 # 如果相差超過50則就認為找到了缺口的位置 for i in range(0,260): for j in range(0,116): if is_similar(image1,image2,i,j)==False: return i #根據缺口的位置模擬x軸移動的軌跡 def get_track(length): pass list=[] #間隔通過隨機范圍函數來獲得,每次移動一步或者兩步 x=random.randint(1,3) #生成軌跡並保存到list內 while length-x>=5: list.append(x) length=length-x x=random.randint(1,3) #最后五步都是一步步移動 for i in range(length): list.append(1) return list #滑動驗證碼破解程序 def main(): #打開火狐瀏覽器 driver = webdriver.Firefox() #用火狐瀏覽器打開網頁 driver.get("http://www.geetest.com/exp_embed") #等待頁面的上元素刷新出來 WebDriverWait(driver, 30).until(lambda the_driver: the_driver.find_element_by_xpath("//div[@class='gt_slider_knob gt_show']").is_displayed()) WebDriverWait(driver, 30).until(lambda the_driver: the_driver.find_element_by_xpath("//div[@class='gt_cut_bg gt_show']").is_displayed()) WebDriverWait(driver, 30).until(lambda the_driver: the_driver.find_element_by_xpath("//div[@class='gt_cut_fullbg gt_show']").is_displayed()) #下載圖片 image1=get_image(driver, "//div[@class='gt_cut_bg gt_show']/div") image2=get_image(driver, "//div[@class='gt_cut_fullbg gt_show']/div") #計算缺口位置 loc=get_diff_location(image1, image2) #生成x的移動軌跡點 track_list=get_track(loc) #找到滑動的圓球 element=driver.find_element_by_xpath("//div[@class='gt_slider_knob gt_show']") location=element.location #獲得滑動圓球的高度 y=location['y'] #鼠標點擊元素並按住不放 print ("第一步,點擊元素") ActionChains(driver).click_and_hold(on_element=element).perform() time.sleep(0.15) print ("第二步,拖動元素") track_string = "" for track in track_list: #不能移動太快,否則會被認為是程序執行 track_string = track_string + "{%d,%d}," % (track, y - 445) #xoffset=track+22:這里的移動位置的值是相對於滑動圓球左上角的相對值,而軌跡變量里的是圓球的中心點,所以要加上圓球長度的一半。 #yoffset=y-445:這里也是一樣的。不過要注意的是不同的瀏覽器渲染出來的結果是不一樣的,要保證最終的計算后的值是22,也就是圓球高度的一半 ActionChains(driver).move_to_element_with_offset(to_element=element, xoffset=track+22, yoffset=y-445).perform() #間隔時間也通過隨機函數來獲得,間隔不能太快,否則會被認為是程序執行 time.sleep(random.randint(10,50)/100) print (track_string) #xoffset=21,本質就是向后退一格。這里退了5格是因為圓球的位置和滑動條的左邊緣有5格的距離 ActionChains(driver).move_to_element_with_offset(to_element=element, xoffset=21, yoffset=y-445).perform() time.sleep(0.1) ActionChains(driver).move_to_element_with_offset(to_element=element, xoffset=21, yoffset=y-445).perform() time.sleep(0.1) ActionChains(driver).move_to_element_with_offset(to_element=element, xoffset=21, yoffset=y-445).perform() time.sleep(0.1) ActionChains(driver).move_to_element_with_offset(to_element=element, xoffset=21, yoffset=y-445).perform() time.sleep(0.1) ActionChains(driver).move_to_element_with_offset(to_element=element, xoffset=21, yoffset=y-445).perform() print ("第三步,釋放鼠標") #釋放鼠標 ActionChains(driver).release(on_element=element).perform() time.sleep(3) #點擊驗證 # submit = driver.find_element_by_xpath("//div[@class='gt_ajax_tip success']") # print(submit.location) # time.sleep(5) #關閉瀏覽器,為了演示方便,暫時注釋掉. #driver.quit() #主函數入口 if __name__ == '__main__': pass main()