用selenium解決滑塊驗證碼的實現(xiàn)步驟
前言
因為種種原因沒能實現(xiàn)愿景的目標(biāo),在這里記錄一下中間結(jié)果,也算是一個收場吧。這篇博客主要是用selenium解決滑塊驗證碼的個別案列。
思路:
- 用selenium打開瀏覽器指定網(wǎng)站
- 將殘缺塊圖片和背景圖片下載到本地
- 對比兩張圖片的相似地方,計算要滑動的距離
- 規(guī)劃路線,移動滑塊
實現(xiàn)步驟
1. 用selenium打開瀏覽器瀏覽指定網(wǎng)站
1.1 找到chromedriver.exe的路徑
點擊開始找到谷歌圖標(biāo)==》右鍵更多==》打開文件位置==》右鍵谷歌快捷方式==》屬性 ==》打開文件所在的位置 ==》復(fù)制路徑
1.2 代碼
from selenium import webdriver # chrome_path要改成你自己的路徑 chrome_path = r"C:\Users\11248\AppData\Local\Google\Chrome\Application\chromedriver.exe" url = 'https://icas.jnu.edu.cn/cas/login' driver = webdriver.Chrome(chrome_path) driver.get(url)
2.將殘缺塊圖片和背景圖片下載到本地
2.1 找到圖片位置
打開網(wǎng)頁進入開發(fā)者工具,找到圖片位置
2.2 代碼
import time import requests from PIL import Image from selenium.webdriver.common.by import By from io import BytesIO time.sleep(5) # 進入頁面要停留幾秒鐘,等頁面加載完 target_link = driver.find_element(By.CLASS_NAME, "yidun_bg-img").get_attribute('src') template_link = driver.find_element(By.CLASS_NAME, "yidun_jigsaw").get_attribute('src') target_img = Image.open(BytesIO(requests.get(target_link).content)) template_img = Image.open(BytesIO(requests.get(template_link).content)) target_img.save('target.jpg') template_img.save('template.png')
3. 對比兩張圖片的相似地方,計算要滑動的距離
3.1 用matchTemplate獲取移動距離
因為背景圖片中的殘缺塊位置和原始?xì)埲眻D的亮度有所差異,直接對比兩張圖片相似的地方,往往得不到令人滿意的結(jié)果,在此要對兩張圖片進行一定的處理,為了避免這種亮度的干擾,筆者這里將兩張圖片先進行灰度處理,再對圖像進行高斯處理,最后進行邊緣檢測。
def handel_img(img): imgGray = cv2.cvtColor(img, cv2.COLOR_RGBA2GRAY) # 轉(zhuǎn)灰度圖 imgBlur = cv2.GaussianBlur(imgGray, (5, 5), 1) # 高斯模糊 imgCanny = cv2.Canny(imgBlur, 60, 60) # Canny算子邊緣檢測 return imgCanny
為增加工作量(放屁,統(tǒng)一代碼好看點) 將JPG圖像轉(zhuǎn)變?yōu)?通道(RGBA)
def add_alpha_channel(img): """ 為jpg圖像添加alpha通道 """ r_channel, g_channel, b_channel = cv2.split(img) # 剝離jpg圖像通道 alpha_channel = np.ones(b_channel.shape, dtype=b_channel.dtype) * 255 # 創(chuàng)建Alpha通道 img_new = cv2.merge((r_channel, g_channel, b_channel, alpha_channel)) # 融合通道 return img_new
3.2 代碼
import cv2 # 讀取圖像 def match(img_jpg_path, img_png_path): # 讀取圖像 img_jpg = cv2.imread(img_jpg_path, cv2.IMREAD_UNCHANGED) img_png = cv2.imread(img_png_path, cv2.IMREAD_UNCHANGED) # 判斷jpg圖像是否已經(jīng)為4通道 if img_jpg.shape[2] == 3: img_jpg = add_alpha_channel(img_jpg) img = handel_img(img_jpg) small_img = handel_img(img_png) res_TM_CCOEFF_NORMED = cv2.matchTemplate(img, small_img, 3) value = cv2.minMaxLoc(res_TM_CCOEFF_NORMED) value = value[3][0] # 獲取到移動距離 return value
3.3 檢驗效果
為了驗證思路和方法是否得當(dāng),這里將滑塊圖片與背景圖片進行拼接,為后面埋下一個小坑。
def merge_img(jpg_img, png_img, y1, y2, x1, x2): """ 將png透明圖像與jpg圖像疊加 y1,y2,x1,x2為疊加位置坐標(biāo)值 """ # 判斷jpg圖像是否已經(jīng)為4通道 if jpg_img.shape[2] == 3: jpg_img = add_alpha_channel(jpg_img) # 獲取要覆蓋圖像的alpha值,將像素值除以255,使值保持在0-1之間 alpha_png = png_img[yy1:yy2, xx1:xx2, 3] / 255.0 alpha_jpg = 1 - alpha_png # 開始疊加 for c in range(0, 3): jpg_img[y1:y2, x1:x2, c] = ((alpha_jpg * jpg_img[y1:y2, x1:x2, c]) + (alpha_png * png_img[yy1:yy2, xx1:xx2, c])) return jpg_img img_jpg_path = 'target.jpg' # 讀者可自行修改文件路徑 img_png_path = 'template.png' # 讀者可自行修改文件路徑 x1 = match(img_jpg_path, img_png_path) y1 = 0 x2 = x1 + img_png.shape[1] y2 = y1 + img_png.shape[0] # 開始疊加 res_img = merge_img(img_jpg, img_png, y1, y2, x1, x2) cv2.imshow("res_img ", res_img) cv2.waitKey(0)
4. 規(guī)劃路線,移動滑塊
4.1 點擊滑塊移動
用第3節(jié)已經(jīng)獲取到的距離,點擊滑塊進行移動
from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.support.wait import WebDriverWait from selenium.webdriver import ActionChains def crack_slider(distance): wait = WebDriverWait(driver, 20) slider = wait.until(EC.element_to_be_clickable((By.CLASS_NAME, 'yidun_slider'))) ActionChains(self.driver).click_and_hold(slider).perform() ActionChains(self.driver).move_by_offset(xoffset=distance, yoffset=0).perform() time.sleep(2) ActionChains(self.driver).release().perform() return 0
神奇的事情是,坑來了,沒有匹配成功。
4.2 匹配失敗原因
這里有以下兩點原因:
- 圖片尺寸發(fā)生了變化,距離要進行轉(zhuǎn)換。
- 滑塊滑動時,滑塊和殘缺塊的相對位置有變動
首先解決圖片尺寸變化問題,找到網(wǎng)頁中圖片大?。?45x172.500
下載到本地圖片大?。?80x240
所以要對距離進行以下處理:
distance = distance / 480 * 345
關(guān)于第二個問題,這里沒有找到很好的測量工具測量出來,好在驗證碼對位置精確度要求不高,就一個個試數(shù)吧。
distance = distance /480 * 345 + 12
5 運行演示
補充
在對極驗驗證碼進行學(xué)習(xí)中,有的網(wǎng)站對移動軌跡進行了驗證,如果滑動太快,也會被識別出機器操作,為了模擬人工操作,出色的程序員寫出了一個魔幻移動軌跡,舉個例子:我們可以先超過目標(biāo),再往回移動。
def get_tracks(distance): distance += 20 v = 0 t = 0.2 forward_tracks = [] current = 0 mid = distance * 3 / 5 while current < distance: if current < mid: a = 2 else: a = -3 s = v * t + 0.5 * a * (t ** 2) v = v + a * t current += s forward_tracks.append(round(s)) back_tracks = [-3, -3, -2, -2, -2, -2, -2, -1, -1, -1] return {'forward_tracks': forward_tracks, 'back_tracks': back_tracks} def crack_slider(tracks): wait = WebDriverWait(driver, 20) slider = wait.until(EC.element_to_be_clickable((By.CLASS_NAME, 'yidun_slider'))) ActionChains(driver).click_and_hold(slider).perform() # 模擬按住鼠標(biāo)左鍵 for track in tracks['forward_tracks']: ActionChains(driver).move_by_offset(xoffset=track, yoffset=0).perform() time.sleep(0.5) for back_tracks in tracks['back_tracks']: ActionChains(driver).move_by_offset(xoffset=back_tracks, yoffset=0).perform() ActionChains(driver).move_by_offset(xoffset=-4, yoffset=0).perform() ActionChains(driver).move_by_offset(xoffset=4, yoffset=0).perform() time.sleep(0.5) ActionChains(driver).release().perform() # 釋放左鍵 return 0
完整代碼
# coding=utf-8 import re import requests import time from io import BytesIO import cv2 import numpy as np from PIL import Image from selenium import webdriver from selenium.webdriver import ActionChains from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.support.wait import WebDriverWait class CrackSlider(): # 通過瀏覽器截圖,識別驗證碼中缺口位置,獲取需要滑動距離,并破解滑動驗證碼 def __init__(self): super(CrackSlider, self).__init__() self.opts = webdriver.ChromeOptions() self.opts.add_experimental_option('excludeSwitches', ['enable-logging']) # self.driver = webdriver.Chrome(ChromeDriverManager().install(), options=self.opts) chrome_path = r"C:\Users\11248\AppData\Local\Google\Chrome\Application\chromedriver.exe" self.driver = webdriver.Chrome(chrome_path, options=self.opts) self.url = 'https://icas.jnu.edu.cn/cas/login' self.wait = WebDriverWait(self.driver, 10) def get_pic(self): self.driver.get(self.url) time.sleep(5) target_link = self.driver.find_element(By.CLASS_NAME, "yidun_bg-img").get_attribute('src') template_link = self.driver.find_element(By.CLASS_NAME, "yidun_jigsaw").get_attribute('src') target_img = Image.open(BytesIO(requests.get(target_link).content)) template_img = Image.open(BytesIO(requests.get(template_link).content)) target_img.save('target.jpg') template_img.save('template.png') def crack_slider(self, distance): slider = self.wait.until(EC.element_to_be_clickable((By.CLASS_NAME, 'yidun_slider'))) ActionChains(self.driver).click_and_hold(slider).perform() ActionChains(self.driver).move_by_offset(xoffset=distance, yoffset=0).perform() time.sleep(2) ActionChains(self.driver).release().perform() return 0 def add_alpha_channel(img): """ 為jpg圖像添加alpha通道 """ r_channel, g_channel, b_channel = cv2.split(img) # 剝離jpg圖像通道 alpha_channel = np.ones(b_channel.shape, dtype=b_channel.dtype) * 255 # 創(chuàng)建Alpha通道 img_new = cv2.merge((r_channel, g_channel, b_channel, alpha_channel)) # 融合通道 return img_new def handel_img(img): imgGray = cv2.cvtColor(img, cv2.COLOR_RGBA2GRAY) # 轉(zhuǎn)灰度圖 imgBlur = cv2.GaussianBlur(imgGray, (5, 5), 1) # 高斯模糊 imgCanny = cv2.Canny(imgBlur, 60, 60) # Canny算子邊緣檢測 return imgCanny def match(img_jpg_path, img_png_path): # 讀取圖像 img_jpg = cv2.imread(img_jpg_path, cv2.IMREAD_UNCHANGED) img_png = cv2.imread(img_png_path, cv2.IMREAD_UNCHANGED) # 判斷jpg圖像是否已經(jīng)為4通道 if img_jpg.shape[2] == 3: img_jpg = add_alpha_channel(img_jpg) img = handel_img(img_jpg) small_img = handel_img(img_png) res_TM_CCOEFF_NORMED = cv2.matchTemplate(img, small_img, 3) value = cv2.minMaxLoc(res_TM_CCOEFF_NORMED) value = value[3][0] # 獲取到移動距離 return value # 1. 打開chromedriver,試試下載圖片 cs = CrackSlider() cs.get_pic() # 2. 對比圖片,計算距離 img_jpg_path = 'target.jpg' # 讀者可自行修改文件路徑 img_png_path = 'template.png' # 讀者可自行修改文件路徑 distance = match(img_jpg_path, img_png_path) distance = distance /480 * 345 + 12 # 3. 移動 cs.crack_slider(distance)
總結(jié)
到此這篇關(guān)于用selenium解決滑塊驗證碼的文章就介紹到這了,更多相關(guān)selenium滑塊驗證碼內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python代碼中引用已經(jīng)寫好的模塊、方法的兩種方式
這篇文章主要介紹了Python代碼中引用已經(jīng)寫好的模塊、方法,下面就介紹兩種方式,可以簡潔明了地調(diào)用自己在其他模塊寫的代碼,需要的朋友可以參考下2022-07-07Python中PyQt5可視化界面通過拖拽來上傳文件的實現(xiàn)
本文主要介紹了Python中PyQt5可視化界面通過拖拽來上傳文件的實現(xiàn),通過構(gòu)建一個可接受拖拽的區(qū)域,并重寫相關(guān)事件處理函數(shù),可以方便地實現(xiàn)文件上傳功能,具有一定的參考價值,感興趣的可以了解一下2023-12-12解決pycharm導(dǎo)入numpy包的和使用時報錯:RuntimeError: The current Numpy ins
這篇文章主要介紹了解決pycharm導(dǎo)入numpy包的和使用時報錯:RuntimeError: The current Numpy installation (‘D:\\python3.6\\lib\\site-packa的問題,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-12-12從訓(xùn)練好的tensorflow模型中打印訓(xùn)練變量實例
今天小編就為大家分享一篇從訓(xùn)練好的tensorflow模型中打印訓(xùn)練變量實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01使用python連接mysql數(shù)據(jù)庫數(shù)據(jù)方式
這篇文章主要介紹了使用python連接mysql數(shù)據(jù)庫數(shù)據(jù)方式,住喲有兩種方式,具體內(nèi)容,需要的小伙伴可以參考下面文章內(nèi)容,希望對你有所幫助2022-03-03