验证码的识别

本文详细介绍了如何识别图形验证码和极验滑动验证码。对于图形验证码,涉及安装pytesseract、图片处理(灰度化、二值化)以识别清晰的验证码。在滑动验证码部分,讲解了魅族网站的验证码识别流程,包括模拟点击验证按钮、识别缺口位置、生成滑动路径并模拟拖动,重点讨论了固定滑动间距的情况和获取缺口位置的算法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一:图形验证码的识别
1.准备工作
①安装pytesseract pip install pytesseract
②在安装过程中存在坑,实际运行时提示路径不在指定位置,处理方式参考
https://blog.youkuaiyun.com/wang_hugh/article/details/80760940
2.识别测试
①图片中数字和字母清晰,无噪声

from PIL import Image
import pytesseract
image=Image.open(r'C:\Users\wcl\Desktop\image.png')
result=pytesseract.image_to_string(image)
print(result)

②验证码图片内存在干扰信息,需要转灰度、二值化、改变二值化的阈值等操作
将图片转化为灰度图像

image=image.convert('L')

将图片进行二值化处理,传入数字1

image=image.convert('1')

有干扰图片的处理

from PIL import Image
import pytesseract
image=Image.open(r'C:\Users\wcl\Desktop\image.png')
image=image.convert('L')
threshold=127
table=[]
for i in range(256):
    if i <threshold:
        table.append(0)
    else:
        table.append(1)
image=image.point(table,'1')
result=pytesseract.image_to_string(image)
print(result)

二:极验滑动验证码的识别
1.流程
包括分析识别思路、识别缺口位置、生成滑块拖动路径、模拟实现滑块拼合通过验证等步骤
2.以魅族网站为例
思路:
①模拟点击验证按钮
②识别滑动缺口的位置
?模拟拖动滑块
3.过程
①第一步比较简单,直接用selenium模拟点击按钮
②识别缺口的位置比较关键,需要用到图像相关的处理方法。
分为若干种情况:
a.有原图和有缺口的图
分别截图原图和有缺口的图,通过对比像素,找到两者之间的距离
b.只有有缺口的图
存在某种规律,比如每类滑动滑块滑动的间距一致,这类只需要将两者间距填入即可
c.只有有缺口的图2
两者间距不一致,存在变化,需要将图片切割,然后再合成,此类比较麻烦(不会)
4.详细介绍前两种验证码的识别
①只有有缺口的图1:滑动间距固定

from selenium import webdriver
import time
from selenium.webdriver.common.action_chains import ActionChains
#模拟拖动
def get_track(distance):
    '''
    根据偏移量获取移动轨迹
    :param distance: 偏移量
    :return: 移动轨迹
    '''
    #移动轨迹
    track=[]
    #当前位移
    current=0
    #减速阈值
    mid=distance*4/5
    #计算间隔
    t=0.2
    #初速度
    v=0
    while current<distance:
        if current<mid:
            a=2
        else:
            a=-2
        #初速度 v0
        v0=v#当前速度v=v0+at
        v=v0+a*t
        #移动距离x=v0t+1/2*a*a*t^2
        move=v0*t+1/2*a*t*t
        #当前位移
        current+=move
        track.append(round(move))
    return track
#每次移动的距离是相等的
def main(distance):
    driver = webdriver.Chrome()
    driver.get('https://www.douban.com/')
    driver.switch_to.frame(0)
    #点击<密码登陆>跳转到需要输入账户和密码的界面
    driver.find_element_by_xpath('/html/body/div[1]/div[1]/ul[1]/li[2]').click()
    driver.find_element_by_xpath('//*[@id="username"]').send_keys('your username')
    driver.find_element_by_xpath('//*[@id="password"]').send_keys('your password')
    #输入账户和密码后,点击登陆豆瓣
    driver.find_element_by_xpath('/html/body/div[1]/div[2]/div[1]/div[5]/a').click()
    #有两种情况,一种不需要验证码直接成功进入主页,另一种是登陆过于频繁,需要输入验证码
    time.sleep(2)
    if '我的豆瓣' in driver.page_source:
        return driver.page_source
    else:
        # 滑块位于iframe框架中
        driver.switch_to.frame(0)
        # 定位图片下面的滑动按钮
        element = driver.find_element_by_xpath('//*[@id="tcaptcha_drag_thumb"]')
        # 点击滑动按钮
        ActionChains(driver).click_and_hold(on_element=element).perform()
        # 拖
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值