就是图中的这种滑块验证码

验证码

先贴源码,基本思路就是 用PIL,然后对比 验证码图片 有缺口和无缺口的不同,计算出偏移量,模拟滑块滑动。

import time
import random
import io
from PIL import Image
from urllib.request import urlopen
from selenium.webdriver.common.action_chains import ActionChains

def openImgUrl(src):
    # 打开图片链接
    ImgUrl = urlopen(src, timeout=1).read()
    img = Image.open(io.BytesIO(ImgUrl))
    # 恢复原始大小
    imgResize = (int(i // 2) for i in img.size)
    return img.resize(imgResize, Image.ANTIALIAS)

def getImg(dr):
    dr.switch_to.frame('tcaptcha_iframe')
    src1 = dr.find_element_by_id('slideBg').get_attribute('src')
    src2 = src1.replace('img_index=1', 'img_index=0')
    return openImgUrl(src1), openImgUrl(src2)

# 比较两张图片同一点上的像数值,差距大于设置标准返回False
def isPixel(img1, img2, x, y):
    i = 100
    px1, px2 = img1.load()[x, y], img2.load()[x, y]
    r, g, b = [abs(p1 - p2) for p1, p2 in zip(px1, px2)]
    return True if r < i and g < i and b < i else False

# 获取缺口的偏移量,两张图片对比,(i,j)像素点的RGB差距,过大则该x为偏移值
def getOffset(img1, img2):
    offset, distance = None, 70
    x, y = img1.size
    for i in range(distance, x):
        for j in range(y):
            if not isPixel(img1, img2, i, j):
                offset = i
                return offset
    return offset

# 计算滑块的移动轨迹 滑块并不是从0开始移动,有一个初始值
def getTrack(offset):
    offset -= 30
    return [offset / 4] * 4

# 模拟释放鼠标抖动
def shake(dr):
    ActionChains(dr).move_by_offset(xoffset=-2, yoffset=0).perform()
    ActionChains(dr).move_by_offset(xoffset=2, yoffset=0).perform()
    time.sleep(random.random())

# 按轨迹移动
def move(dr, track):
    for i in track:
        ActionChains(dr).move_by_offset(xoffset=i, yoffset=0).perform()
        time.sleep(random.random() / 100)
    time.sleep(random.random())

# 完成拖动操作
def sliderBtn(dr, track):
    btn = dr.find_element_by_id('tcaptcha_drag_thumb')
    ActionChains(dr).click_and_hold(btn).perform()  # 按住按钮不放
    move(dr, track)  # 按正向轨迹移动
    move(dr, [-1, -0.5, -1])  # 按逆向轨迹移动
    shake(dr)  # 模拟人手抖动
    ActionChains(dr).release().perform()  # 松开滑块按钮

# 处理验证码
def dealCaptcha(dr):
    time.sleep(3)
    img1, img2 = getImg(dr)
    offset = getOffset(img1, img2)
    track = getTrack(offset)
    sliderBtn(dr, track)

基于selenium的webdriver


from selenium import webdriver

if __name__ == '__main__':
    dr = webdriver.Chrome('chromedriver的地址')
    dealCaptcha(dr)
`