滑块识别验证

滑块识别

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)
相关推荐
全栈开发圈22 分钟前
新书速览|Java网络爬虫精解与实践
java·开发语言·爬虫
小白学大数据27 分钟前
JavaScript重定向对网络爬虫的影响及处理
开发语言·javascript·数据库·爬虫
Python大数据分析@30 分钟前
python操作CSV和excel,如何来做?
开发语言·python·excel
黑叶白树31 分钟前
简单的签到程序 python笔记
笔记·python
Shy96041844 分钟前
Bert完形填空
python·深度学习·bert
上海_彭彭1 小时前
【提效工具开发】Python功能模块执行和 SQL 执行 需求整理
开发语言·python·sql·测试工具·element
zhongcx011 小时前
使用Python查找大文件的实用脚本
python
ac-er88881 小时前
PHP网络爬虫常见的反爬策略
开发语言·爬虫·php
yyfhq2 小时前
sdnet
python
测试19982 小时前
2024软件测试面试热点问题
自动化测试·软件测试·python·测试工具·面试·职场和发展·压力测试