最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

用selenium解決滑塊驗證碼的實現(xiàn)步驟

 更新時間:2023年02月14日 10:06:02   作者:騎著青蛙一起二  
驗證碼作為一種自然人的機器人的判別工具,被廣泛的用于各種防止程序做自動化的場景中,下面這篇文章主要給大家介紹了關(guān)于用selenium解決滑塊驗證碼的實現(xiàn)步驟,需要的朋友可以參考下

前言

因為種種原因沒能實現(xiàn)愿景的目標,在這里記錄一下中間結(jié)果,也算是一個收場吧。這篇博客主要是用selenium解決滑塊驗證碼的個別案列。

思路:

  • 用selenium打開瀏覽器指定網(wǎng)站
  • 將殘缺塊圖片和背景圖片下載到本地
  • 對比兩張圖片的相似地方,計算要滑動的距離
  • 規(guī)劃路線,移動滑塊

實現(xiàn)步驟

1. 用selenium打開瀏覽器瀏覽指定網(wǎng)站

1.1 找到chromedriver.exe的路徑

點擊開始找到谷歌圖標==》右鍵更多==》打開文件位置==》右鍵谷歌快捷方式==》屬性 ==》打開文件所在的位置 ==》復制路徑

chromedriver.exe路徑

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獲取移動距離

因為背景圖片中的殘缺塊位置和原始殘缺圖的亮度有所差異,直接對比兩張圖片相似的地方,往往得不到令人滿意的結(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 檢驗效果

為了驗證思路和方法是否得當,這里將滑塊圖片與背景圖片進行拼接,為后面埋下一個小坑。

def merge_img(jpg_img, png_img, y1, y2, x1, x2):
    """ 將png透明圖像與jpg圖像疊加
        y1,y2,x1,x2為疊加位置坐標值
    """
    # 判斷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 運行演示

success

補充

在對極驗驗證碼進行學習中,有的網(wǎng)站對移動軌跡進行了驗證,如果滑動太快,也會被識別出機器操作,為了模擬人工操作,出色的程序員寫出了一個魔幻移動軌跡,舉個例子:我們可以先超過目標,再往回移動。

 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() # 模擬按住鼠標左鍵

      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)文章

  • Django使用Celery異步任務隊列的使用

    Django使用Celery異步任務隊列的使用

    這篇文章主要介紹了Django使用Celery異步任務隊列的使用,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-03-03
  • Python繪制直方圖的示例代碼

    Python繪制直方圖的示例代碼

    本文主要介紹了如何使用Python繪制直方圖,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-07-07
  • Python生成器(Generator)詳解

    Python生成器(Generator)詳解

    這篇文章主要介紹了Python生成器(Generator)詳解,本文講解了什么是生成器、簡單生成器、帶yield 語句的生成器、加強的生成器等內(nèi)容,需要的朋友可以參考下
    2015-04-04
  • Python?編程操作連載之字符串,列表,字典和集合處理

    Python?編程操作連載之字符串,列表,字典和集合處理

    這篇文章主要介紹了Python?編程操作連載之字符串,列表,字典和集合處理,文章圍繞主題相關(guān)資料展開詳細的內(nèi)容介紹,需要的朋友可參考一下下面文章內(nèi)容
    2022-06-06
  • Python?3.11.0下載安裝并使用help查看模塊信息的方法

    Python?3.11.0下載安裝并使用help查看模塊信息的方法

    本文給大家介紹Python?3.11.0下載安裝并使用help查看模塊信息的相關(guān)知識,首先給大家講解了Python?3.11.0下載及安裝緊接著介紹了在命令行使用help查看模塊信息的方法,感興趣的朋友跟隨小編一起看看吧
    2022-11-11
  • Python做個自定義動態(tài)壁紙還可以放視頻

    Python做個自定義動態(tài)壁紙還可以放視頻

    這篇文章主要介紹了如何用Python做個可以放視頻自定義動態(tài)壁紙,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-08-08
  • 解決pip install中UnicodeDecodeError問題的處理

    解決pip install中UnicodeDecodeError問題的處理

    這篇文章主要介紹了解決pip install中UnicodeDecodeError問題的處理,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-09-09
  • 深入了解和應用Python 裝飾器 @decorator

    深入了解和應用Python 裝飾器 @decorator

    在編程過程中,經(jīng)常遇到這樣的場景:登錄校驗,權(quán)限校驗,日志記錄等,這些功能代碼在各個環(huán)節(jié)都可能需要,但又十分雷同,通過裝飾器來抽象、剝離這部分代碼可以很好解決這類場景,這篇文章主要介紹了Python的裝飾器 @decorator,探討了使用的方式,需要的朋友可以參考下
    2019-04-04
  • python對網(wǎng)頁文本的格式化實例方法

    python對網(wǎng)頁文本的格式化實例方法

    在本篇文章里小編給大家整理是一篇關(guān)于python對網(wǎng)頁文本的格式化實例方法,有興趣的朋友們可以跟著學習參考下。
    2021-10-10
  • python打開使用的方法

    python打開使用的方法

    在本篇文章里小編給各位整理的是關(guān)于python怎么打開使用的相關(guān)知識點內(nèi)容,有需要的朋友們可以學習下。
    2019-09-09

最新評論

中宁县| 和静县| 兖州市| 瑞丽市| 邵阳市| 鸡泽县| 隆回县| 尤溪县| 康平县| 南岸区| 昔阳县| 勃利县| 泰兴市| 原阳县| 兴文县| 静乐县| 文水县| 集安市| 轮台县| 阿瓦提县| 舞阳县| 越西县| 耒阳市| 北流市| 察雅县| 拉萨市| 南城县| 西城区| 韩城市| 永吉县| 铜梁县| 工布江达县| 宜州市| 广灵县| 城固县| 亳州市| 塔河县| 富川| 富宁县| 崇信县| 宁德市|