艺赛旗RPA验证码处理系列(三):破解极验滑动验证码

目前艺赛旗RPA已经更新到8.0版本,可以让所有用户免费下载试用http://www.i-search.com.cn/index.html?from=line1 (复制链接下载)

一,介绍
一些网站会在正常的账号密码认证之外加一些验证码,以此来明确地区分人/机行为,从一定程度上达到反爬的效果,对于简单的校验码Tesserocr就可以搞定,如下
在这里插入图片描述
在这里插入图片描述
但一些网站加入了滑动验证码,最典型的要属于极验滑动认证了,极验官网:http://www.geetest.com/,下图是极验的登录界面

现在极验验证码已经更新到了3.0版本,截至2017年7月全球已有十六万家企业正在使用极验,每天服务响应超过四亿次,广泛应用于直播视频,金融服务,电子商务,游戏娱乐,政府企业等各大类型网站

对于这类验证,如果我们直接模拟表单请求,繁琐的认证参数与认证流程会让你蛋碎一地,我们可以用selenium驱动浏览器来解决这个问题,大致分为以下几个步骤

#1,输入账号,密码,然后点击登陆
#2,点击按钮,弹出没有缺口的图
#3,针对没有缺口的图片进行截图
#4,点击滑动按钮,弹出有缺口的图
#5,针对有缺口的图片进行截图
#6,对比两张图片,找出缺口,即滑动的位移
#7,按照人的行为行为习惯,把总位移切成一段段小的位移
#8,按照位移移动
#9,完成登录

二,实现
安装:selenium+chrome/phantomjs
安装:Pillow

这里用的是Chrome
Pillow:基于PIL,处理python 3.x的图形图像库。因为PIL只能处理到python 2.x,而这个模块能处理Python3.x,目前用它做图形的很多。
http://www.cnblogs.com/apexchu/p/4231041.html

C:\Users\Administrator>pip3 install pillow
C:\Users\Administrator>python3
Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 18:41:36) [MSC v.1900 64 bit (AMD64)] on win32
Type “help”, “copyright”, “credits” or “license” for more information.

from PIL import Image

代码如下(增加部分注释)
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from PIL import Image
import time

def get_snap():
‘’’
对整个网页截图,保存成图片,然后用PIL.Image拿到图片对象
:return: 图片对象
‘’’
driver.save_screenshot(‘snap.png’)
page_snap_obj=Image.open(‘snap.png’)
return page_snap_obj

def get_image():
‘’’
从网页的网站截图中,截取验证码图片
:return: 验证码图片
‘’’
img=wait.until(EC.presence_of_element_located((By.CLASS_NAME,‘geetest_canvas_img’)))
time.sleep(2) #保证图片刷新出来
localtion=img.location
size=img.size

top=localtion['y']
bottom=localtion['y']+size['height']
left=localtion['x']
right=localtion['x']+size['width']

page_snap_obj=get_snap()
crop_imag_obj=page_snap_obj.crop((left,top,right,bottom))
return crop_imag_obj

def get_distance(image1,image2):
‘’’
拿到滑动验证码需要移动的距离
:param image1:没有缺口的图片对象
:param image2:带缺口的图片对象
:return:需要移动的距离
#两张图大小一样,那就通过两个for循环依次对比每个像素点的RGB值
#如果相差超过60(threshold)则就认为找到了缺口的位置
‘’’
threshold = 60 #色差值
left = 57 #起始位置(开始进行RGB色差判断的最小值)
for i in range(left,image1.size[0]):
for j in range(image1.size[1]):
rgb1=image1.load()[i,j]
rgb2=image2.load()[i,j]
res1=abs(rgb1[0]-rgb2[0])
res2=abs(rgb1[1]-rgb2[1])
res3=abs(rgb1[2]-rgb2[2])
# 如果相差超过60(threshold)则就认为找到了缺口的位置
if not (res1 < threshold and res2 < threshold and res3 < threshold):
return i-7 #经过测试,误差为大概为7
return i-7 #经过测试,误差为大概为7
def get_tracks(dista

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值