欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

OpenCV結(jié)合selenium實現(xiàn)滑塊驗證碼

 更新時間:2021年08月13日 10:12:02   作者:GuanLu  
本文主要介紹了OpenCV結(jié)合selenium實現(xiàn)滑塊驗證碼,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

本次案例使用OpenCV和selenium來解決一下滑塊驗證碼

先說一下思路:

  • 彈出滑塊驗證碼后使用selenium元素截圖將驗證碼整個背景圖截取出來
  • 將需要滑動的小圖單獨截取出來,最好將小圖與背景圖頂部的像素距離獲取到,這樣可以將背景圖上下多余的邊框截取掉
  • 使用OpenCV將背景圖和小圖進(jìn)行灰度處理,并對小圖再次進(jìn)行二值化全局閾值,這樣就可以利用OpenCV在背景圖中找到小圖所在的位置
  • 用OpenCV獲取到相差的距離后利用selenium的鼠標(biāo)拖動方法進(jìn)行拖拉至終點。

詳細(xì)步驟:

先獲取驗證碼背景圖,selenium瀏覽器對象中使用screenshot方法可以將指定的元素圖片截取出來

import os
from selenium import webdriver


browser = webdriver.Chrome()
browser.get("https://www.toutiao.com/c/user/token/MS4wLjABAAAA4EKNlqVeNTTuEdWn0VytNS8cdODKTsNNwLTxOnigzZtclro2Kylvway5mTyTUKvz/")

save_path = os.path.join(os.path.expanduser('~'), "Desktop", "background.png")
browser.find_element_by_id("element_id_name").screenshot(save_path)

截取后的驗證碼背景圖和需要滑動的小圖   如:

再將小圖與背景圖頂部的像素距離獲取到,指的是下面圖中紅邊的高度:

如果HTML元素中小圖是單獨存在時,那么它的高度在會定義在頁面元素中,使用selenium頁面元素對象的value_of_css_property方法可以獲取到像素距離。

獲取這個是因為要把背景圖的上下兩邊多余部分進(jìn)行切除,從而保留關(guān)鍵的圖像部位,能夠大幅度提高識別率。

element_object = browser.find_element_by_xpath("xpath_element")
px = element_object.value_of_css_property("top")

接下來就要對圖像進(jìn)行灰度處理:

import numpy
import cv2


def make_threshold(img):
    """全局閾值
    將圖片二值化,去除噪點,讓其黑白分明"""
    x = numpy.ones(img.shape, numpy.uint8) * 255
    y = img - x
    result, thresh = cv2.threshold(y, 127, 255, cv2.THRESH_BINARY_INV)
    # 將二值化后的結(jié)果返回
    return thresh


class ComputeDistance:
    """獲取需要滑動的距離
    將驗證碼背景大圖和需要滑動的小圖進(jìn)行處理,先在大圖中找到相似的小圖位置,再獲取對應(yīng)的像素偏移量"""
    def __init__(self, Background_path: str, image_to_move: str, offset_top_px: int):
        """
        :param Background_path: 驗證碼背景大圖
        :param image_to_move: 需要滑動的小圖
        :param offset_top_px: 小圖距離在大圖上的頂部邊距(像素偏移量)
        """
        self.Background_img = cv2.imread(Background_path)
        self.offset_px = offset_top_px
        self.show_img = show_img
        small_img_data = cv2.imread(image_to_move, cv2.IMREAD_UNCHANGED)
        # 得到一個改變維度為50的乘以值
        scaleX = 50 / small_img_data.shape[1]
        # 使用最近鄰插值法縮放,讓xy乘以scaleX,得到縮放后shape為50x50的圖片
        self.tpl_img = cv2.resize(small_img_data, (0, 0), fx=scaleX, fy=scaleX)
        self.Background_cutting = None

    def tpl_op(self):
        # 將小圖轉(zhuǎn)換為灰色
        tpl_gray = cv2.cvtColor(self.tpl_img, cv2.COLOR_BGR2GRAY)
        h, w = tpl_gray.shape
        # 將背景圖轉(zhuǎn)換為灰色
        # Background_gray = cv2.cvtColor(self.Background_img, cv2.COLOR_BGR2GRAY)
        Background_gray = cv2.cvtColor(self.Background_cutting, cv2.COLOR_BGR2GRAY)
        # 得到二值化后的小圖
        threshold_img = make_threshold(tpl_gray)
        # 將小圖與大圖進(jìn)行模板匹配,找到所對應(yīng)的位置
        result = cv2.matchTemplate(Background_gray, threshold_img, cv2.TM_CCOEFF_NORMED)
        min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)
        # 左上角位置
        top_left = (max_loc[0] - 5, max_loc[1] + self.offset_px)
        # 右下角位置
        bottom_right = (top_left[0] + w, top_left[1] + h)
        # 在源顏色大圖中畫出小圖需要移動到的終點位置
        """rectangle(圖片源數(shù)據(jù), 左上角, 右下角, 顏色, 畫筆厚度)"""
        cv2.rectangle(self.Background_img, top_left, bottom_right, (0, 0, 255), 2)

    def cutting_background(self):
        """切割圖片的上下邊框"""
        height = self.tpl_img.shape[0]
        # 將大圖中上下多余部分去除,如: Background_img[40:110, :]
        self.Background_cutting = self.Background_img[self.offset_px - 10: self.offset_px + height + 10, :]

    def run(self):
        # 如果小圖的長度與大圖的長度一致則不用將大圖進(jìn)行切割,可以將self.cutting_background()注釋掉
        self.cutting_background()
        return self.tpl_op()


if __name__ == '__main__':
    image_path1 = "背景圖路徑"
    image_path2 = "小圖路徑"
    distance_px = "像素距離"
    main = ComputeDistance(image_path1, image_path2, distance_px)
    main.run()

上面代碼可以返回小圖到凹點的距離,現(xiàn)在我們可以看一下灰度處理中的圖片樣子:

得到距離后還要對這個距離數(shù)字進(jìn)行處理一下,要讓它拆分成若干個小數(shù),這么做的目的是在拖動的時候不能一下拖動到終點,

要模仿人類的手速緩緩向前行駛,不然很明顯是機(jī)器在操控。

比如到終點的距離為100,那么要把它轉(zhuǎn)為 [8, 6, 11, 10, 3, 6, 3, -2, 4, 0, 15, 1, 9, 6, -2, 4, 1, -2, 15, 6, -2] 類似的,列表中的數(shù)加起來正好為100.

最簡單的轉(zhuǎn)換:

def handle_distance(distance):
    """將直線距離轉(zhuǎn)為緩慢的軌跡"""
    import random
    slow_distance = []
    while sum(slow_distance) <= distance:
        slow_distance.append(random.randint(-2, 15))

    if sum(slow_distance) != distance:
        slow_distance.append(distance - sum(slow_distance))
    return slow_distance

有了到終點的距離,接下來就開始拖動吧:

import time
from random import randint
from selenium.webdriver.common.action_chains import ActionChains


def move_slider(website, slider, track, **kwargs):
    """將滑塊移動到終點位置
    :param website: selenium頁面對象
    :param slider: selenium頁面中滑塊元素對象
    :param track: 到終點所需的距離
    """
    name = kwargs.get('name', '滑塊')

    try:
        if track[0] > 200:
            return track[0]
        # 點擊滑塊元素并拖拽
        ActionChains(website).click_and_hold(slider).perform()
        time.sleep(0.15)
        for i in track:
            # 隨機(jī)上下浮動鼠標(biāo)
            ActionChains(website).move_by_offset(xoffset=i, yoffset=randint(-2, 2)).perform()
        # 釋放元素
        time.sleep(1)
        ActionChains(website).release(slider).perform()
        time.sleep(1)
        # 隨機(jī)拿開鼠標(biāo)
        ActionChains(website).move_by_offset(xoffset=randint(200, 300), yoffset=randint(200, 300)).perform()
        print(f'[網(wǎng)頁] 拖拽 {name}')
        return True
    except Exception as e:
        print(f'[網(wǎng)頁] 拖拽 {name} 失敗 {e}')

教程結(jié)束,讓我們結(jié)合上面代碼做一個案例吧。

訪問今日頭條某博主的主頁,直接打開主頁的鏈接會出現(xiàn)驗證碼。

下面代碼 使用pip安裝好相關(guān)依賴庫后可直接運行:

調(diào)用ComputeDistance類時,參數(shù) show_img=True 可以在拖動驗證碼前進(jìn)行展示背景圖識別終點后的區(qū)域在哪里, 如:

distance_obj = ComputeDistance(background_path, small_path, px, show_img=True)

OK,下面為案例代碼: 

import os
import time
import requests
import cv2
import numpy
from random import randint

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains


def show_image(img_array, name='img', resize_flag=False):
    """展示圖片"""
    maxHeight = 540
    maxWidth = 960
    scaleX = maxWidth / img_array.shape[1]
    scaleY = maxHeight / img_array.shape[0]
    scale = min(scaleX, scaleY)
    if resize_flag and scale < 1:
        img_array = cv2.resize(img_array, (0, 0), fx=scale, fy=scale)
    cv2.imshow(name, img_array)
    cv2.waitKey(0)
    cv2.destroyWindow(name)


def make_threshold(img):
    """全局閾值
    將圖片二值化,去除噪點,讓其黑白分明"""
    x = numpy.ones(img.shape, numpy.uint8) * 255
    y = img - x
    result, thresh = cv2.threshold(y, 127, 255, cv2.THRESH_BINARY_INV)
    # 將二值化后的結(jié)果返回
    return thresh


def move_slider(website, slider, track, **kwargs):
    """將滑塊移動到終點位置
    :param website: selenium頁面對象
    :param slider: selenium頁面中滑塊元素對象
    :param track: 到終點所需的距離
    """
    name = kwargs.get('name', '滑塊')

    try:
        if track[0] > 200:
            return track[0]
        # 點擊滑塊元素并拖拽
        ActionChains(website).click_and_hold(slider).perform()
        time.sleep(0.15)
        for i in track:
            # 隨機(jī)上下浮動鼠標(biāo)
            ActionChains(website).move_by_offset(xoffset=i, yoffset=randint(-2, 2)).perform()
        # 釋放元素
        time.sleep(1)
        ActionChains(website).release(slider).perform()
        time.sleep(1)
        # 隨機(jī)拿開鼠標(biāo)
        ActionChains(website).move_by_offset(xoffset=randint(200, 300), yoffset=randint(200, 300)).perform()
        print(f'[網(wǎng)頁] 拖拽 {name}')
        return True
    except Exception as e:
        print(f'[網(wǎng)頁] 拖拽 {name} 失敗 {e}')


class ComputeDistance:
    """獲取需要滑動的距離
    將驗證碼背景大圖和需要滑動的小圖進(jìn)行處理,先在大圖中找到相似的小圖位置,再獲取對應(yīng)的像素偏移量"""
    def __init__(self, Background_path: str, image_to_move: str, offset_top_px: int, show_img=False):
        """
        :param Background_path: 驗證碼背景大圖
        :param image_to_move: 需要滑動的小圖
        :param offset_top_px: 小圖距離在大圖上的頂部邊距(像素偏移量)
        :param show_img: 是否展示圖片
        """
        self.Background_img = cv2.imread(Background_path)
        self.offset_px = offset_top_px
        self.show_img = show_img
        small_img_data = cv2.imread(image_to_move, cv2.IMREAD_UNCHANGED)
        # 得到一個改變維度為50的乘以值
        scaleX = 50 / small_img_data.shape[1]
        # 使用最近鄰插值法縮放,讓xy乘以scaleX,得到縮放后shape為50x50的圖片
        self.tpl_img = cv2.resize(small_img_data, (0, 0), fx=scaleX, fy=scaleX)
        self.Background_cutting = None

    def show(self, img):
        if self.show_img:
            show_image(img)

    def tpl_op(self):
        # 將小圖轉(zhuǎn)換為灰色
        tpl_gray = cv2.cvtColor(self.tpl_img, cv2.COLOR_BGR2GRAY)
        h, w = tpl_gray.shape
        # 將背景圖轉(zhuǎn)換為灰色
        # Background_gray = cv2.cvtColor(self.Background_img, cv2.COLOR_BGR2GRAY)
        Background_gray = cv2.cvtColor(self.Background_cutting, cv2.COLOR_BGR2GRAY)
        # 得到二值化后的小圖
        threshold_img = make_threshold(tpl_gray)
        # 將小圖與大圖進(jìn)行模板匹配,找到所對應(yīng)的位置
        result = cv2.matchTemplate(Background_gray, threshold_img, cv2.TM_CCOEFF_NORMED)
        min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)
        # 左上角位置
        top_left = (max_loc[0] - 5, max_loc[1] + self.offset_px)
        # 右下角位置
        bottom_right = (top_left[0] + w, top_left[1] + h)
        # 在源顏色大圖中畫出小圖需要移動到的終點位置
        """rectangle(圖片源數(shù)據(jù), 左上角, 右下角, 顏色, 畫筆厚度)"""
        cv2.rectangle(self.Background_img, top_left, bottom_right, (0, 0, 255), 2)
        if self.show_img:
            show_image(self.Background_img)
        return top_left

    def cutting_background(self):
        """切割圖片的上下邊框"""
        height = self.tpl_img.shape[0]
        # 將大圖中上下多余部分去除,如: Background_img[40:110, :]
        self.Background_cutting = self.Background_img[self.offset_px - 10: self.offset_px + height + 10, :]

    def run(self):
        # 如果小圖的長度與大圖的長度一致則不用將大圖進(jìn)行切割,可以將self.cutting_background()注釋掉
        self.cutting_background()
        return self.tpl_op()


class TodayNews(object):
    def __init__(self):
        self.url = "https://www.toutiao.com/c/user/token/" \
                   "MS4wLjABAAAA4EKNlqVeNTTuEdWn0VytNS8cdODKTsNNwLTxOnigzZtclro2Kylvway5mTyTUKvz/"
        self.process_folder = os.path.join(os.path.expanduser('~'), "Desktop", "today_news")
        self.background_path = os.path.join(self.process_folder, "background.png")
        self.small_path = os.path.join(self.process_folder, "small.png")
        self.small_px = None
        self.xpath = {}
        self.browser = None

    def check_file_exist(self):
        """檢查流程目錄是否存在"""
        if not os.path.isdir(self.process_folder):
            os.mkdir(self.process_folder)

    def start_browser(self):
        """啟動瀏覽器"""
        self.browser = webdriver.Chrome()
        self.browser.maximize_window()

    def close_browser(self):
        self.browser.quit()

    def wait_element_loaded(self, xpath: str, timeout=10, close_browser=True):
        """等待頁面元素加載完成
        :param xpath: xpath表達(dá)式
        :param timeout: 最長等待超時時間
        :param close_browser: 元素等待超時后是否關(guān)閉瀏覽器
        :return: Boolean
        """
        now_time = int(time.time())
        while int(time.time()) - now_time < timeout:
            # noinspection PyBroadException
            try:
                element = self.browser.find_element_by_xpath(xpath)
                if element:
                    return True
                time.sleep(1)
            except Exception:
                pass
        else:
            if close_browser:
                self.close_browser()
            # print("查找頁面元素失敗,如果不存在網(wǎng)絡(luò)問題請嘗試修改xpath表達(dá)式")
            return False

    def add_page_element(self):
        self.xpath['background_img'] = '//div[@role="dialog"]/div[2]/img[1]'
        self.xpath['small_img'] = '//div[@role="dialog"]/div[2]/img[2]'
        self.xpath['slider_button'] = '//div[@id="secsdk-captcha-drag-wrapper"]/div[2]'

    def process_main(self):
        """處理頁面內(nèi)容"""
        self.browser.get(self.url)

        for _ in range(10):
            if self.wait_element_loaded(self.xpath['background_img'], timeout=5, close_browser=False):
                time.sleep(1)
                # 截圖
                self.browser.find_element_by_xpath(self.xpath['background_img']).screenshot(self.background_path)
                small_img = self.browser.find_element_by_xpath(self.xpath['small_img'])
                # 獲取小圖片的URL鏈接
                small_url = small_img.get_attribute("src")
                # 獲取小圖片距離背景圖頂部的像素距離
                self.small_px = small_img.value_of_css_property("top").replace("px", "").split(".")[0]

                response = requests.get(small_url)
                if response.ok:
                    with open(self.small_path, "wb") as file:
                        file.write(response.content)

                time.sleep(1)
                # 如果沒滑動成功則刷新頁面重試
                if not self.process_slider():
                    self.browser.refresh()
                    continue
            else:
                break

    @staticmethod
    def handle_distance(distance):
        """將直線距離轉(zhuǎn)為緩慢的軌跡"""
        import random
        slow_distance = []
        while sum(slow_distance) <= distance:
            slow_distance.append(random.randint(-2, 15))

        if sum(slow_distance) != distance:
            slow_distance.append(distance - sum(slow_distance))
        return slow_distance

    def process_slider(self):
        """處理滑塊驗證碼"""

        distance_obj = ComputeDistance(self.background_path, self.small_path, int(self.small_px), show_img=False)
        # 獲取移動所需的距離
        distance = distance_obj.run()

        track = self.handle_distance(distance[0])
        track.append(-2)
        slider_element = self.browser.find_element_by_xpath(self.xpath['slider_button'])

        move_slider(self.browser, slider_element, track)
        time.sleep(2)

        # 如果滑動完成則返回True
        if not self.wait_element_loaded(self.xpath['slider_button'], timeout=2, close_browser=False):
            return True
        else:
            return False

    def run(self):
        self.check_file_exist()
        self.start_browser()
        self.add_page_element()
        self.process_main()
        # self.close_browser()


if __name__ == '__main__':
    main = TodayNews()
    main.run()

到此這篇關(guān)于OpenCV結(jié)合selenium實現(xiàn)滑塊驗證碼的文章就介紹到這了,更多相關(guān)OpenCV selenium滑塊驗證碼內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • python中內(nèi)置函數(shù)range詳解

    python中內(nèi)置函數(shù)range詳解

    Python內(nèi)置函數(shù)range()是一個用于生成一系列連續(xù)的整數(shù)的函數(shù),它常用于循環(huán)結(jié)構(gòu)中,用于指定循環(huán)的次數(shù)或迭代的范圍,這篇文章主要介紹了python之內(nèi)置函數(shù)range,需要的朋友可以參考下
    2023-07-07
  • python導(dǎo)出hive數(shù)據(jù)表的schema實例代碼

    python導(dǎo)出hive數(shù)據(jù)表的schema實例代碼

    這篇文章主要介紹了python導(dǎo)出hive數(shù)據(jù)表的schema實例代碼,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下
    2018-01-01
  • Python實現(xiàn)二叉樹的常見遍歷操作總結(jié)【7種方法】

    Python實現(xiàn)二叉樹的常見遍歷操作總結(jié)【7種方法】

    這篇文章主要介紹了Python實現(xiàn)二叉樹的常見遍歷操作,結(jié)合實例形式總結(jié)分析了二叉樹的前序、中序、后序、層次遍歷中的迭代與遞歸等7種操作方法,需要的朋友可以參考下
    2019-03-03
  • Python編程使用PyQt5庫實現(xiàn)動態(tài)水波進(jìn)度條示例

    Python編程使用PyQt5庫實現(xiàn)動態(tài)水波進(jìn)度條示例

    這篇文章主要介紹了Python編程使用PyQt5庫實現(xiàn)動態(tài)水波進(jìn)度條的示例代碼解析,有需要的朋友可以借鑒參考下希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪
    2021-10-10
  • python實現(xiàn)盲盒抽獎功能(減庫存)

    python實現(xiàn)盲盒抽獎功能(減庫存)

    本文主要介紹了python實現(xiàn)盲盒抽獎功能,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • Python編程django實現(xiàn)同一個ip十分鐘內(nèi)只能注冊一次

    Python編程django實現(xiàn)同一個ip十分鐘內(nèi)只能注冊一次

    這篇文章主要介紹了Python編程django實現(xiàn)同一個ip十分鐘內(nèi)只能注冊一次的相關(guān)內(nèi)容,具有一定參考價值。需要的朋友可以了解下。
    2017-11-11
  • 解析django的csrf跨站請求偽造

    解析django的csrf跨站請求偽造

    本文主要介紹了解析django的csrf跨站請求偽造,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-08-08
  • python回溯算法實現(xiàn)全排列小練習(xí)分享

    python回溯算法實現(xiàn)全排列小練習(xí)分享

    這篇文章主要給大家分享的是python回溯算法實現(xiàn)全排列小練習(xí),文章根據(jù)例子:輸入列表L(不含重復(fù)元素),輸出L的全排列展開學(xué)習(xí),需要的小伙伴可以參考一下
    2022-02-02
  • python數(shù)字圖像處理圖像的繪制詳解

    python數(shù)字圖像處理圖像的繪制詳解

    這篇文章主要為大家介紹了python數(shù)字圖像處理圖像的繪制示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-06-06
  • Pandas中Apply函數(shù)加速百倍的技巧分享

    Pandas中Apply函數(shù)加速百倍的技巧分享

    雖然目前dask,cudf等包的出現(xiàn),使得我們的數(shù)據(jù)處理大大得到了加速,但是并不是每個人都有比較好的gpu。本文為大家分享幾個Pandas中Apply函數(shù)加速百倍的技巧,希望有所幫助
    2022-07-07

最新評論