selenium+Chrome滑動(dòng)驗(yàn)證碼破解二(某某網(wǎng)站)
具體詳情見代碼,研究網(wǎng)站,隨便輸入手機(jī)號(hào)點(diǎn)擊獲取驗(yàn)證碼
在自己寫代碼前參考了一批博客,是把所有驗(yàn)證碼圖片截取所有驗(yàn)證碼圖片保存在本地,再對(duì)比,感覺方法不行,所以自己寫了個(gè)破解方法,通過js修改css直接抓取完整圖片,因?yàn)樯弦黄獙懥薆站,這里就不一一分析了,直接上代碼:
破解成功界面
完整代碼:
# -*- coding:utf-8 -*- ''' 研究網(wǎng)站: https://account.ch.com/NonRegistrations-Regist 滑塊驗(yàn)證碼也分兩種: 1.直接給缺口圖片,先滑動(dòng)到缺口找到完整驗(yàn)證碼圖片,對(duì)比有缺口的驗(yàn)證碼圖片,然后計(jì)算缺口坐標(biāo),再利用selenium移動(dòng)按鈕到指定位置 2.直接給原圖,缺口需要點(diǎn)擊出現(xiàn),直接保存原圖,然后對(duì)比 ''' from selenium import webdriver from selenium.webdriver.support.wait import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By from selenium.webdriver import ActionChains from lxml.html import etree from PIL import Image from time import sleep import re, requests from urllib.request import urlretrieve from bs4 import BeautifulSoup class SliderVerificationCode(object): def __init__(self): # 初始化一些信息 self.left = 60 # 定義一個(gè)左邊的起點(diǎn) 缺口一般離圖片左側(cè)有一定的距離 有一個(gè)滑塊 self.url = 'https://account.ch.com/NonRegistrations-Regist' self.chromedriverPath = "C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe" self.driver = webdriver.Chrome(executable_path=self.chromedriverPath) self.wait = WebDriverWait(self.driver, 20) # 設(shè)置等待時(shí)間20秒 self.phone = "18516544488" def input_name_password(self): # 輸入手機(jī)號(hào) self.driver.get(self.url) self.driver.maximize_window() self.inputphone = self.wait.until(EC.presence_of_element_located((By.NAME, 'phoneNumberInput'))) self.inputphone.send_keys(self.phone) def click_login_button(self): # 點(diǎn)擊登錄按鈕,出現(xiàn)驗(yàn)證碼圖片 login_button = self.wait.until(EC.element_to_be_clickable((By.ID, 'getDynamicPwd'))) login_button.click() sleep(1) def get_geetest_image(self): # 獲取驗(yàn)證碼圖片 # print(self.driver.page_source) gapimg = self.wait.until(EC.presence_of_element_located((By.CLASS_NAME, 'geetest_canvas_bg'))) sleep(2) gapimg.screenshot(r'./captcha1.png') # 通過js代碼修改標(biāo)簽樣式 顯示圖片2 js = 'var change = document.getElementsByClassName("geetest_canvas_fullbg");change[0].style = "display:block;"' self.driver.execute_script(js) sleep(2) fullimg = self.wait.until( EC.presence_of_element_located((By.CLASS_NAME, 'geetest_canvas_fullbg'))) fullimg.screenshot(r'./captcha2.png') def is_similar(self, image1, image2, x, y): '''判斷兩張圖片 各個(gè)位置的像素是否相同 #image1:帶缺口的圖片 :param image2: 不帶缺口的圖片 :param x: 位置x :param y: 位置y :return: (x,y)位置的像素是否相同 ''' # 獲取兩張圖片指定位置的像素點(diǎn) pixel1 = image1.load()[x, y] pixel2 = image2.load()[x, y] # 設(shè)置一個(gè)閾值 允許有誤差 threshold = 60 # 彩色圖 每個(gè)位置的像素點(diǎn)有三個(gè)通道 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_diff_location(self): # 獲取缺口圖起點(diǎn) captcha1 = Image.open('captcha1.png') captcha2 = Image.open('captcha2.png') for x in range(self.left, captcha1.size[0]): # 從左到右 x方向 for y in range(captcha1.size[1]): # 從上到下 y方向 if not self.is_similar(captcha1, captcha2, x, y): return x # 找到缺口的左側(cè)邊界 在x方向上的位置 def get_move_track(self, gap): track = [] # 移動(dòng)軌跡 current = 0 # 當(dāng)前位移 # 減速閾值 mid = gap * 4 / 5 # 前4/5段加速 后1/5段減速 t = 0.2 # 計(jì)算間隔 v = 0 # 初速度 while current < gap: if current < mid: a = 3 # 加速度為+3 else: a = -3 # 加速度為-3 v0 = v # 初速度v0 v = v0 + a * t # 當(dāng)前速度 move = v0 * t + 1 / 2 * a * t * t # 移動(dòng)距離 current += move # 當(dāng)前位移 track.append(round(move)) # 加入軌跡 return track def move_slider(self, track): slider = self.wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, '.geetest_slider_button'))) ActionChains(self.driver).click_and_hold(slider).perform() for x in track: # 只有水平方向有運(yùn)動(dòng) 按軌跡移動(dòng) ActionChains(self.driver).move_by_offset(xoffset=x, yoffset=0).perform() sleep(1) ActionChains(self.driver).release().perform() # 松開鼠標(biāo) def main(self): self.input_name_password() self.click_login_button() self.get_geetest_image() gap = self.get_diff_location() # 缺口左起點(diǎn)位置 gap = gap - 6 # 減去滑塊左側(cè)距離圖片左側(cè)在x方向上的距離 即為滑塊實(shí)際要移動(dòng)的距離 track = self.get_move_track(gap) print("移動(dòng)軌跡", track) self.move_slider(track) if __name__ == "__main__": springAutumn = SliderVerificationCode() springAutumn.main()
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
python3跳出一個(gè)循環(huán)的實(shí)例操作
在本篇內(nèi)容里小編給大家整理的是關(guān)于python3跳出一個(gè)循環(huán)的實(shí)例操作內(nèi)容,有需要的朋友們可以參考下。2020-08-08Python 實(shí)現(xiàn)任意區(qū)域文字識(shí)別(OCR)操作
這篇文章主要介紹了Python 實(shí)現(xiàn)任意區(qū)域文字識(shí)別(OCR)操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-03-03flask中使用藍(lán)圖將路由分開寫在不同文件實(shí)例解析
這篇文章主要介紹了flask中使用藍(lán)圖將路由分開寫在不同文件實(shí)例解析,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-01-01Python數(shù)學(xué)建模PuLP庫線性規(guī)劃實(shí)際案例編程詳解
本節(jié)以一個(gè)實(shí)際數(shù)學(xué)建模案例,來為大家講解PuLP求解線性規(guī)劃問題的建模與編程。來鞏固加深大家對(duì)Python數(shù)學(xué)建模PuLP庫線性規(guī)劃的運(yùn)用理解2021-10-10pycharm設(shè)置python文件模板信息過程圖解
這篇文章主要介紹了pycharm設(shè)置python文件模板信息過程圖解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03Python使用Phantomjs截屏網(wǎng)頁的方法
今天小編就為大家分享一篇Python使用Phantomjs截屏網(wǎng)頁的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-05-05