滑块识别验证

本文介绍了如何利用Python和Selenium库配合DdddOcr库实现网站滑块验证码的自动化识别过程,包括图片下载、坐标计算以及模拟滑动操作。
摘要由CSDN通过智能技术生成

滑块识别

1. 获取图片

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

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

# 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. 获取背景图片与缺口图片

# 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

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/

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滑动

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. 完整代码

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)
  • 21
    点赞
  • 44
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

林小果呀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值