滑块识别验证

滑块识别

1. 获取图片

测试网站:https://www.geetest.com/adaptive-captcha-demo

2. 点击滑块拼图并开始验证

python 复制代码
# 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()

3. 获取背景图片与缺口图片

python 复制代码
# 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)


# 5.读取缺口图片
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)

4. 识别图片

背景图: https://static.geetest.com/captcha_v4/e70fbf1d77/slide/491f18e9b8/2022-04-21T09/bg/031bfe44f51149da9e7adbecfbdac599.png

缺口图: https://static.geetest.com/captcha_v4/e70fbf1d77/slide/491f18e9b8/2022-04-21T09/slice/031bfe44f51149da9e7adbecfbdac599.png

4.1 ddddocr

python 复制代码
import ddddocr
import requests

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)  # 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)  # 196 12 276 92

4.2 打码平台

网址:http://www.ttshitu.com/

python 复制代码
import base64
import requests

bg_bytes = requests.get(bg_image_url).content
b64_string = base64.b64encode(bg_bytes).decode('utf-8')

data = {"username": "自己的用户名", "password": "自己的密码", "typeid": 33, "image": b64_string}
res = requests.post("http://api.ttshitu.com/predict", json=data)
data_dict = res.json()
distance = data_dict['data'] # 只返回横坐标
print(distance)  # {'result': '173', 'id': 't0pNdrJjTCmXScFtGRofaw'}

5. Selenium滑动

python 复制代码
from selenium.webdriver import ActionChains

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

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

time.sleep(3)

6. 完整代码

python 复制代码
import re
import time

import ddddocr
import requests
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait

driver = webdriver.Edge()

# 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)

# 5.识别图片坐标
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)  # 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)  # 196 12 276 92

# 6.滑动滑块
tag = driver.find_element(By.CLASS_NAME, 'geetest_btn')
time.sleep(2)
ActionChains(driver).click_and_hold(tag).perform()  # 点击并抓住标签
ActionChains(driver).move_by_offset(xoffset=x1, yoffset=0).perform()  # 向右滑动114像素(向左是负数)
ActionChains(driver).release().perform()  # 释放

time.sleep(3)
相关推荐
零度@19 分钟前
Java中Map的多种用法
java·前端·python
大数据魔法师20 分钟前
昆明天气数据分析与挖掘(一)- 昆明天气数据采集
爬虫·网络爬虫
550A26 分钟前
如何修改kagglehub的数据集默认下载路径
python
倚天仗剑走天涯WGM30 分钟前
对CANoe和VBA和TSmaster 三款工具的调用的理解
python
我命由我1234531 分钟前
python-dotenv - python-dotenv 快速上手
服务器·开发语言·数据库·后端·python·学习·学习方法
黑客思维者31 分钟前
Python定时任务schedule/APScheduler/Crontab 原理与落地实践
开发语言·python·crontab·apscheduler
yaoxin52112336 分钟前
268. Java Stream API 入门指南
java·开发语言·python
测试人社区-千羽41 分钟前
48小时攻克测试岗——闪电面试极速备战手册
人工智能·python·opencv·面试·职场和发展·单元测试·压力测试
weixin_390308461 小时前
Python工具网站网址
python
电饭叔1 小时前
指定私有浮点数据域get/set方法等《python语言程序设计》2018版--第8章18题第1部分
开发语言·python