首页 > 其他分享 >09-验证-滑块

09-验证-滑块

时间:2024-02-11 09:33:17浏览次数:32  
标签:bg 滑块 验证 image 09 driver geetest tag import

image-20231206081923147

基于selenium实现过滑块验证核心需要三步:

  • 获取验证码图片
  • 识别图片,计算轨迹距离
  • 寻找滑块,控制滑动

1.获取图片

https://www.geetest.com/adaptive-captcha-demo

image-20231206093404644

image-20231206093434520

示例1:

import re
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.wait import WebDriverWait

service = Service("driver/chromedriver.exe")
driver = webdriver.Chrome(service=service)

# 1.打开首页
driver.get('https://www.geetest.com/adaptive-captcha-demo')

# 2.点击【滑动拼图验证】
tag = WebDriverWait(driver, 30, 0.5).until(lambda dv: dv.find_element(
    By.XPATH,
    '//*[@id="gt-showZh-mobile"]/div/section/div/div[2]/div[1]/div[2]/div[3]/div[3]'
))
tag.click()

# 3.点击开始验证
tag = WebDriverWait(driver, 30, 0.5).until(lambda dv: dv.find_element(
    By.CLASS_NAME,
    'geetest_btn_click'
))
tag.click()


# 4.读取背景图片
def fetch_bg_func(dv):
    tag_object = dv.find_element(
        By.CLASS_NAME,
        'geetest_bg'
    )
    style_string = tag_object.get_attribute("style")
    match_list = re.findall('url\(\"(.*)\"\);', style_string)  # ["http..." ]     []
    if match_list:
        return match_list[0]


bg_image_url = WebDriverWait(driver, 30, 0.5).until(fetch_bg_func)  # 新的函数 = 某个函数('geetest_bg')
print("背景图:", bg_image_url)


# 4.读取缺口图片
def fetch_slice_func(dv):
    tag_object = dv.find_element(
        By.CLASS_NAME,
        'geetest_slice_bg'
    )
    style_string = tag_object.get_attribute("style")
    match_list = re.findall('url\(\"(.*)\"\);', style_string)
    if match_list:
        return match_list[0]


slice_image_url = WebDriverWait(driver, 30, 0.5).until(fetch_slice_func) # 新的函数 = 某个函数('geetest_slice_bg')
print("缺口图:", slice_image_url)

time.sleep(2000)
driver.close()

示例2:闭包

import re
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.wait import WebDriverWait

service = Service("driver/chromedriver.exe")
driver = webdriver.Chrome(service=service)

# 1.打开首页
driver.get('https://www.geetest.com/adaptive-captcha-demo')

# 2.点击【滑动拼图验证】
tag = WebDriverWait(driver, 30, 0.5).until(lambda dv: dv.find_element(
    By.XPATH,
    '//*[@id="gt-showZh-mobile"]/div/section/div/div[2]/div[1]/div[2]/div[3]/div[3]'
))
tag.click()

# 3.点击开始验证
tag = WebDriverWait(driver, 30, 0.5).until(lambda dv: dv.find_element(
    By.CLASS_NAME,
    'geetest_btn_click'
))
tag.click()


# 4.读取背景图片
def fetch_image_func(class_name):
    def inner(dv):
        tag_object = dv.find_element(
            By.CLASS_NAME,
            class_name
        )
        style_string = tag_object.get_attribute("style")
        match_list = re.findall('url\(\"(.*)\"\);', style_string)
        if match_list:
            return match_list[0]

    return inner


bg_image_url = WebDriverWait(driver, 30, 0.5).until(   fetch_image_func("geetest_bg")   )   # inner函数   class_name="geetest_bg"
print("背景图:", bg_image_url)

# 4.读取缺口图片
slice_image_url = WebDriverWait(driver, 30, 0.5).until(  fetch_image_func("geetest_slice_bg")  ) # inner函数  class_name="geetest_slice_bg"
print("缺口图:", slice_image_url)

time.sleep(2000)
driver.close()

2.识别图片

识别图片中,缺口左边的横坐标(滑动的距离)。

背景图: 
	https://static.geetest.com/captcha_v4/e70fbf1d77/slide/0af8d91d43/2022-04-21T09/bg/33a8f24a9b234a599036569c9e54a76a.png
缺口图: 
	https://static.geetest.com/captcha_v4/e70fbf1d77/slide/0af8d91d43/2022-04-21T09/slice/33a8f24a9b234a599036569c9e54a76a.png

2.1 ddddocr

import ddddocr
import requests

slice_bytes = requests.get("缺口图片地址").content
bg_bytes = requests.get("背景图片地址").content


slide = ddddocr.DdddOcr(det=False, ocr=False, show_ad=False)
res = slide.slide_match(slice_bytes, bg_bytes, simple_target=True)
x1, y1, x2, y2 = res['target']
print(x1, y1, x2, y2)  # 114 45 194 125

2.2 opencv

import cv2
import numpy as np
import requests


def get_distance(bg_bytes, slice_bytes):
    def get_image_object(byte_image):
        img_buffer_np = np.frombuffer(byte_image, dtype=np.uint8)
        img_np = cv2.imdecode(img_buffer_np, 1)
        bg_img = cv2.cvtColor(img_np, cv2.COLOR_BGR2GRAY)
        return bg_img

    bg_image_object = get_image_object(bg_bytes)
    slice_image_object = get_image_object(slice_bytes)
    # 边缘检测
    bg_edge = cv2.Canny(bg_image_object, 255, 255)
    tp_edge = cv2.Canny(slice_image_object, 255, 255)

    bg_pic = cv2.cvtColor(bg_edge, cv2.COLOR_GRAY2RGB)
    tp_pic = cv2.cvtColor(tp_edge, cv2.COLOR_GRAY2RGB)

    res = cv2.matchTemplate(bg_pic, tp_pic, cv2.TM_CCOEFF_NORMED)

    min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)  # 寻找最优匹配
    x = max_loc[0]
    return x


slice_bytes = requests.get("缺口图片地址").content
bg_bytes = requests.get("背景图片地址").content
distance = get_distance(bg_bytes, slice_bytes)
print(distance)

2.3 打码平台

http://www.ttshitu.com/

image-20231206094837737

# 一、图片文字类型(默认 3 数英混合):
# 1 : 纯数字
# 1001:纯数字2
# 2 : 纯英文
# 1002:纯英文2
# 3 : 数英混合
# 1003:数英混合2
#  4 : 闪动GIF
# 7 : 无感学习(独家)
# 11 : 计算题
# 1005:  快速计算题
# 16 : 汉字
# 32 : 通用文字识别(证件、单据)
# 66:  问答题
# 49 :recaptcha图片识别
# 二、图片旋转角度类型:
# 29 :  旋转类型
#
# 三、图片坐标点选类型:
# 19 :  1个坐标
# 20 :  3个坐标
# 21 :  3 ~ 5个坐标
# 22 :  5 ~ 8个坐标
# 27 :  1 ~ 4个坐标
# 48 : 轨迹类型
#
# 四、缺口识别
# 18 : 缺口识别(需要2张图 一张目标图一张缺口图)
# 33 : 单缺口识别(返回X轴坐标 只需要1张图)
# 五、拼图识别
# 53:拼图识别
import base64
import requests

bg_bytes = requests.get("背景图地址").content
b64_string = base64.b64encode(bg_bytes).decode('utf-8')

data = {"username": "wupeiqi", "password": "自己的密码", "typeid": 33, "image":b64_string }
res = requests.post("http://api.ttshitu.com/predict", json=data)
data_dict = res.json()
distance = data_dict['data']['result']
print(distance)
# {"success":true,"code":"0","message":"success","data":{"result":"136","id":"ztAkFAn1RvOJGsFhiAPuWg"}}

3.Selenium滑动

from selenium.webdriver import ActionChains

tag = driver.find_element(By.CLASS_NAME, 'geetest_btn')

ActionChains(driver).click_and_hold(tag).perform()                     # 点击并抓住标签
ActionChains(driver).move_by_offset(xoffset=114, yoffset=0).perform()  # 向右滑动114像素(向左是负数)
ActionChains(driver).release().perform()                               # 释放

4.案例:极验滑块

import re
import time
import ddddocr
import requests
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver import ActionChains

service = Service("driver/chromedriver.exe")
driver = webdriver.Chrome(service=service)

# 1.打开首页
driver.get('https://www.geetest.com/adaptive-captcha-demo')

# 2.点击【滑动拼图验证】
tag = WebDriverWait(driver, 30, 0.5).until(lambda dv: dv.find_element(
    By.XPATH,
    '//*[@id="gt-showZh-mobile"]/div/section/div/div[2]/div[1]/div[2]/div[3]/div[3]'
))
tag.click()

# 3.点击开始验证
tag = WebDriverWait(driver, 30, 0.5).until(lambda dv: dv.find_element(
    By.CLASS_NAME,
    'geetest_btn_click'
))
tag.click()


# 4.读取背景图片
def fetch_image_func(class_name):
    def inner(dv):
        tag_object = dv.find_element(
            By.CLASS_NAME,
            class_name
        )
        style_string = tag_object.get_attribute("style")
        match_list = re.findall('url\(\"(.*)\"\);', style_string)
        if match_list:
            return match_list[0]

    return inner


bg_image_url = WebDriverWait(driver, 30, 0.5).until(fetch_image_func("geetest_bg"))
slice_image_url = WebDriverWait(driver, 30, 0.5).until(fetch_image_func("geetest_slice_bg"))

slice_bytes = requests.get(slice_image_url).content
bg_bytes = requests.get(bg_image_url).content

slide = ddddocr.DdddOcr(det=False, ocr=False, show_ad=False)
res = slide.slide_match(slice_bytes, bg_bytes, simple_target=True)
x1, y1, x2, y2 = res['target']
print("滑动距离",x1)


def show_func(dv):
    geetest_box_tag = dv.find_element(By.CLASS_NAME, "geetest_box")
    display_string = geetest_box_tag.get_attribute("style")
    if "block" in display_string:
        time.sleep(2)
        return dv.find_element(By.CLASS_NAME, 'geetest_btn')


btn_tag = WebDriverWait(driver, 30, 0.5).until(show_func)

ActionChains(driver).click_and_hold(btn_tag).perform()  # 点击并抓住标签
ActionChains(driver).move_by_offset(xoffset=x1, yoffset=0).perform()  # 向右滑动114像素(向左是负数)
ActionChains(driver).release().perform()

time.sleep(2000)
driver.close()

标签:bg,滑块,验证,image,09,driver,geetest,tag,import
From: https://www.cnblogs.com/fuminer/p/18013175

相关文章

  • 10-验证-中文识别点选
    1.获取图片#@课程:爬虫逆向实战课#@讲师:武沛齐#@课件获取:wupeiqi666importreimporttimeimportddddocrimportrequestsfromseleniumimportwebdriverfromselenium.webdriver.common.byimportByfromselenium.webdriver.chrome.serviceimport......
  • P4090 [USACO17DEC] Greedy Gift Takers P
    原题链接题解1.如果前\(7\)头牛能全部能拿到礼物,但是这前\(7\)头牛里有\(4\)头牛更新在前\(4\)的位置,请问第\(8\)头牛能否得到礼物?答案是不行,因为前\(4\)头牛会在前\(4\)的位置形成循环2.假如恰好第\(x\)头牛没有礼物,那么牛\(x\)之后的牛都得不到礼物,因为不......
  • .NET&AI技术分享日活动-202309
    OntheafternoonofSeptember23,2023,Iorganizedtheeighth.NETTechnologySharingDayeventinJinan,ShandongProvince,China.Theeventmainlycoveredfivetechnologicalareas:common.NETtechnologies,low-codeplatforms,front-endframeworks,big......
  • ABC 309
    直接从F开。F三维偏序。把盒子按\(h_i\)排序,离散化,正常跑三维偏序(注意不能相等)。还要处理\(h_i\)相等的情况,可以再把\(h_i\)从大到小排序,然后\(w_i,d_i\)都要求严格大于,如果发现有一种情况是无论\(h_i\)咋排序都可以的,就删掉这种情况。G错排问题的推广。tjtj2......
  • Codeforces Round 909 (Div. 3)
    题目链接A.若n是3的倍数,那么输出第二,因为不管先手如何操作,后手只要跟先手出的相反那么先手就永远不会赢其他情况,先手都能赢#include<bits/stdc++.h>usingnamespacestd;#defineintlonglongconstintN=1e5+10;voidsolve(){intn;cin>>n;if((n+1)%3==0|......
  • 09_TIM输入捕获
    TIM输入捕获输入捕获简介频率测量输入捕获通道主从触发模式输入捕获基本结构PWMI基本结构输入捕获模式测频率接线图引脚选择代码IC.c#include"stm32f10x.h"//DeviceheadervoidIC_Init(void){ RCC_APB1PeriphClockCmd(RCC_APB1Per......
  • [Blazor WebAssembly] 学习随笔——身份验证
    最近在折腾微信相关的开发,包括公众号、企业微信内部应用、企业微信第三方开发。基于Razor方式写了:企业微信内部应用的类库企业微信第三方应用的类库公众号的类库一个统一管理公众号、企业微信内部应用和第三方应用有关授权、Token之类的应用。然后准备写一个开源的简单的酒......
  • 滑块解锁-scratch编程作品
    程序说明:《滑块解锁》是一款基于Scratch平台制作的益智类小游戏。游戏中存在多个黄色滑块阻挡红色滑块通往出口的路径。玩家需要通过逻辑思考和精确操作,滑动黄色滑块以开辟道路,使红色滑块顺利从出口脱出。随着游戏关卡的递进,难度逐渐增加,为玩家带来更大的挑战。滑块解锁源码免......
  • 表单验证功能的实现
     Form组件提供了表单验证的功能,只需要通过 rules 属性传入约定的验证规则,并将Form-Item的 prop 属性设置为需校验的字段名即可。校验规则参见 async-validator<el-form:model="ruleForm":rules="rules">     <el-form-itemlabel="账号" placeholder="......
  • 软件测试学习笔记丨UI_ai自动化获取图片验证码
    UI自动化获取图片验证码代码test_ai.pyfromtimeimportsleepfromseleniumimportwebdriverfromselenium.webdriver.common.byimportByfromL5.AICode.ocr_codeimportOCRCodeclassTestAi:defsetup_class(self):self.driver=webdriver.Chrome()......