一、介绍

   一些网站会在正常的账号密码认证之外加一些验证码,以此来明确地区分人/机行为,从一定程度上达到反爬的效果,对于简单的校验码Tesserocr就可以搞定,如下

破解极验滑动验证码

    但一些网站加入了滑动验证码,最典型的要属于极验滑动认证了,极验官网:http://www.geetest.com/,下图是极验的登录界面

破解极验滑动验证码

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

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

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

二、实现

安装:selenium+chrome/phantomjs

#安装:Pillow
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
>>>
  1 from selenium import webdriver
  2 from selenium.webdriver import ActionChains
  3 from selenium.webdriver.common.by import By
  4 from selenium.webdriver.common.keys import Keys
  5 from selenium.webdriver.support import expected_conditions as EC
  6 from selenium.webdriver.support.wait import WebDriverWait
  7 from PIL import Image
  8 import time
  9 
 10 def get_snap():
 11     '''
 12     对整个网页截图,保存成图片,然后用PIL.Image拿到图片对象
 13     :return: 图片对象
 14     '''
 15     driver.save_screenshot('snap.png')
 16     page_snap_obj=Image.open('snap.png')
 17     return page_snap_obj
 18 
 19 def get_image():
 20     '''
 21     从网页的网站截图中,截取验证码图片
 22     :return: 验证码图片
 23     '''
 24     img=wait.until(EC.presence_of_element_located((By.CLASS_NAME,'geetest_canvas_img')))
 25     time.sleep(2) #保证图片刷新出来
 26     localtion=img.location
 27     size=img.size
 28 
 29     top=localtion['y']
 30     bottom=localtion['y']+size['height']
 31     left=localtion['x']
 32     right=localtion['x']+size['width']
 33 
 34     page_snap_obj=get_snap()
 35     crop_imag_obj=page_snap_obj.crop((left,top,right,bottom))
 36     return crop_imag_obj
 37 
 38 
 39 def get_distance(image1,image2):
 40     '''
 41     拿到滑动验证码需要移动的距离
 42     :param image1:没有缺口的图片对象
 43     :param image2:带缺口的图片对象
 44     :return:需要移动的距离
 45     '''
 46     threshold=60
 47     left=57
 48     for i in range(left,image1.size[0]):
 49         for j in range(image1.size[1]):
 50             rgb1=image1.load()[i,j]
 51             rgb2=image2.load()[i,j]
 52             res1=abs(rgb1[0]-rgb2[0])
 53             res2=abs(rgb1[1]-rgb2[1])
 54             res3=abs(rgb1[2]-rgb2[2])
 55             if not (res1 < threshold and res2 < threshold and res3 < threshold):
 56                 return i-7 #经过测试,误差为大概为7
 57     return i-7 #经过测试,误差为大概为7
 58 
 59 
 60 def get_tracks(distance):
 61     '''
 62     拿到移动轨迹,模仿人的滑动行为,先匀加速后匀减速
 63     匀变速运动基本公式:
 64     ①v=v0+at
 65     ②s=v0t+½at²
 66     ③v²-v0²=2as
 67 
 68     :param distance: 需要移动的距离
 69     :return: 存放每0.3秒移动的距离
 70     '''
 71     #初速度
 72     v=0
 73     #单位时间为0.2s来统计轨迹,轨迹即0.2内的位移
 74     t=0.3
 75     #位移/轨迹列表,列表内的一个元素代表0.2s的位移
 76     tracks=[]
 77     #当前的位移
 78     current=0
 79     #到达mid值开始减速
 80     mid=distance*4/5
 81 
 82     while current < distance:
 83         if current < mid:
 84             # 加速度越小,单位时间的位移越小,模拟的轨迹就越多越详细
 85             a= 2
 86         else:
 87             a=-3
 88 
 89         #初速度
 90         v0=v
 91         #0.2秒时间内的位移
 92         s=v0*t+0.5*a*(t**2)
 93         #当前的位置
 94         current+=s
 95         #添加到轨迹列表
 96         tracks.append(round(s))
 97 
 98         #速度已经达到v,该速度作为下次的初速度
 99         v=v0+a*t
100     return tracks
101 
102 
103 try:
104     driver=webdriver.Chrome()
105     driver.get('https://account.geetest.com/login')
106     wait=WebDriverWait(driver,10)
107 
108     #步骤一:先点击按钮,弹出没有缺口的图片
109     button=wait.until(EC.presence_of_element_located((By.CLASS_NAME,'geetest_radar_tip')))
110     button.click()
111 
112     #步骤二:拿到没有缺口的图片
113     image1=get_image()
114 
115     #步骤三:点击拖动按钮,弹出有缺口的图片
116     button=wait.until(EC.presence_of_element_located((By.CLASS_NAME,'geetest_slider_button')))
117     button.click()
118 
119     #步骤四:拿到有缺口的图片
120     image2=get_image()
121 
122     # print(image1,image1.size)
123     # print(image2,image2.size)
124 
125     #步骤五:对比两张图片的所有RBG像素点,得到不一样像素点的x值,即要移动的距离
126     distance=get_distance(image1,image2)
127 
128     #步骤六:模拟人的行为习惯(先匀加速拖动后匀减速拖动),把需要拖动的总距离分成一段一段小的轨迹
129     tracks=get_tracks(distance)
130     print(tracks)
131     print(image1.size)
132     print(distance,sum(tracks))
133 
134 
135     #步骤七:按照轨迹拖动,完全验证
136     button=wait.until(EC.presence_of_element_located((By.CLASS_NAME,'geetest_slider_button')))
137     ActionChains(driver).click_and_hold(button).perform()
138     for track in tracks:
139         ActionChains(driver).move_by_offset(xoffset=track,yoffset=0).perform()
140     else:
141         ActionChains(driver).move_by_offset(xoffset=3,yoffset=0).perform() #先移过一点
142         ActionChains(driver).move_by_offset(xoffset=-3,yoffset=0).perform() #再退回来,是不是更像人了
143 
144     time.sleep(0.5) #0.5秒后释放鼠标
145     ActionChains(driver).release().perform()
146 
147 
148     #步骤八:完成登录
149     input_email=driver.find_element_by_id('email')
150     input_password=driver.find_element_by_id('password')
151     button=wait.until(EC.element_to_be_clickable((By.CLASS_NAME,'login-btn')))
152 
153     input_email.send_keys('18611453110@163.com')
154     input_password.send_keys('linhaifeng123')
155     # button.send_keys(Keys.ENTER)
156     button.click()
157 
158     import time
159     time.sleep(200)
160 finally:
161     driver.close()
View Code

相关文章: