1、极验滑动验证码原理
' I/ m& @" R/ C1 m% ]: H7 e
; m) C, R9 @: n0 M9 e: E' V! R以上图片是最典型的要属于极验滑动认证了,极验官网:http://www.geetest.com/。. d( e7 Z, D1 b- [5 i
现在极验验证码已经更新到了 3.0 版本,截至 2017 年 7 月全球已有十六万家企业正在使用极验,每天服务响应超过四亿次,广泛应用于直播视频、金融服务、电子商务、游戏娱乐、政府企业等各大类型网站
" \8 c; Q7 g8 t0 j& }2 [0 S对于这类验证,如果我们直接模拟表单请求,繁琐的认证参数与认证流程会让你蛋碎一地,我们可以用selenium驱动浏览器来解决这个问题,大致分为以下几个步骤:, `" T* d) u, c' e3 Z
1、输入用户名,密码; f3 l( _- T: r# e7 y9 W
2、点击按钮验证,弹出没有缺口的图
5 O) K$ L) E( S; \* P, z" `8 t3、获得没有缺口的图片
, V+ o: e) T6 N! o0 @. _4、点击滑动按钮,弹出有缺口的图4 b3 {! e7 c7 m
5、获得有缺口的图片6 {# x1 t. l0 _4 M
6、对比两张图片,找出缺口,即滑动的位移
# s# C- o' L* R3 S6 _, }* x7、按照人的行为行为习惯,把总位移切成一段段小的位移9 U4 U6 l) N3 I6 J, G2 ^2 [
8、按照位移移动' R2 B) M! T5 ^$ \4 n
9、完成登录
$ U( ]& F4 U/ ?$ C, E- k8 h, t/ e7 G
2、位移移动需要的基础知识
4 [7 |. I3 `4 F t% V位移移动相当于匀变速直线运动,类似于小汽车从起点开始运行到终点的过程(首先为匀加速,然后再匀减速)。
2 p" x2 p/ n9 j( r
: Q9 n, h; C$ W1 u% z其中a为加速度,且为恒量(即单位时间内的加速度是不变的),t为时间
- e, P" E! j5 v; u
6 {1 ]+ _) Z+ W
% K' t \7 n' M3 ?7 a1 r
位移移动的代码实现
7 `' \/ q ~+ X" t9 ?- D
- def get_track(distance):
- '''
- 拿到移动轨迹,模仿人的滑动行为,先匀加速后匀减速
- 匀变速运动基本公式:
- ①v=v0+at
- ②s=v0t+(1/2)at²
- ③v²-v0²=2as
-
- :param distance: 需要移动的距离
- :return: 存放每0.2秒移动的距离
- '''
- # 初速度
- v=0
- # 单位时间为0.2s来统计轨迹,轨迹即0.2内的位移
- t=0.1
- # 位移/轨迹列表,列表内的一个元素代表0.2s的位移
- tracks=[]
- # 当前的位移
- current=0
- # 到达mid值开始减速
- mid=distance * 4/5
-
- distance += 10 # 先滑过一点,最后再反着滑动回来
-
- while current < distance:
- if current < mid:
- # 加速度越小,单位时间的位移越小,模拟的轨迹就越多越详细
- a = 2 # 加速运动
- else:
- a = -3 # 减速运动
-
- # 初速度
- v0 = v
- # 0.2秒时间内的位移
- s = v0*t+0.5*a*(t**2)
- # 当前的位置
- current += s
- # 添加到轨迹列表
- tracks.append(round(s))
-
- # 速度已经达到v,该速度作为下次的初速度
- v= v0+a*t
-
- # 反着滑动到大概准确位置
- for i in range(3):
- tracks.append(-2)
- for i in range(4):
- tracks.append(-1)
- return tracks
对比两张图片,找出缺口1 r/ ~' ^4 n: x) B$ q* Y5 `* P
- def get_distance(image1,image2):
- '''
- 拿到滑动验证码需要移动的距离
- :param image1:没有缺口的图片对象
- :param image2:带缺口的图片对象
- :return:需要移动的距离
- '''
- # print('size', image1.size)
-
- threshold = 50
- for i in range(0,image1.size[0]): # 260
- for j in range(0,image1.size[1]): # 160
- pixel1 = image1.getpixel((i,j))
- pixel2 = image2.getpixel((i,j))
- res_R = abs(pixel1[0]-pixel2[0]) # 计算RGB差
- res_G = abs(pixel1[1] - pixel2[1]) # 计算RGB差
- res_B = abs(pixel1[2] - pixel2[2]) # 计算RGB差
- if res_R > threshold and res_G > threshold and res_B > threshold:
- return i # 需要移动的距离
获得图片
6 t6 b/ F3 F$ r2 {0 d9 k- def merge_image(image_file,location_list):
- """
- 拼接图片
- :param image_file:
- :param location_list:
- :return:
- """
- im = Image.open(image_file)
- im.save('code.jpg')
- new_im = Image.new('RGB',(260,116))
- # 把无序的图片 切成52张小图片
- im_list_upper = []
- im_list_down = []
- # print(location_list)
- for location in location_list:
- # print(location['y'])
- if location['y'] == -58: # 上半边
- im_list_upper.append(im.crop((abs(location['x']),58,abs(location['x'])+10,116)))
- if location['y'] == 0: # 下半边
- im_list_down.append(im.crop((abs(location['x']),0,abs(location['x'])+10,58)))
-
- x_offset = 0
- for im in im_list_upper:
- new_im.paste(im,(x_offset,0)) # 把小图片放到 新的空白图片上
- x_offset += im.size[0]
-
- x_offset = 0
- for im in im_list_down:
- new_im.paste(im,(x_offset,58))
- x_offset += im.size[0]
- new_im.show()
- return new_im
-
- def get_image(driver,div_path):
- '''
- 下载无序的图片 然后进行拼接 获得完整的图片
- :param driver:
- :param div_path:
- :return:
- '''
- time.sleep(2)
- background_images = driver.find_elements_by_xpath(div_path)
- location_list = []
- for background_image in background_images:
- location = {}
- result = re.findall('background-image: url\("(.*?)"\); background-position: (.*?)px (.*?)px;',background_image.get_attribute('style'))
- # print(result)
- location['x'] = int(result[0][1])
- location['y'] = int(result[0][2])
-
- image_url = result[0][0]
- location_list.append(location)
-
- print('==================================')
- image_url = image_url.replace('webp','jpg')
- # '替换url http://static.geetest.com/pictures/gt/579066de6/579066de6.webp'
- image_result = requests.get(image_url).content
- # with open('1.jpg','wb') as f:
- # f.write(image_result)
- image_file = BytesIO(image_result) # 是一张无序的图片
- image = merge_image(image_file,location_list)
-
- return image
按照位移移动8 j0 [+ ]! {& @$ G
- print('第一步,点击滑动按钮')
- ActionChains(driver).click_and_hold(on_element=element).perform() # 点击鼠标左键,按住不放
- time.sleep(1)
- print('第二步,拖动元素')
- for track in track_list:
- ActionChains(driver).move_by_offset(xoffset=track, yoffset=0).perform() # 鼠标移动到距离当前位置(x,y)
- if l<100:
- ActionChains(driver).move_by_offset(xoffset=-2, yoffset=0).perform()
- else:
- ActionChains(driver).move_by_offset(xoffset=-5, yoffset=0).perform()
- time.sleep(1)
- print('第三步,释放鼠标')
- ActionChains(driver).release(on_element=element).perform()
详细代码6 Z( c' }4 Y, p, `) i$ n, A3 R
- from selenium import webdriver
- from selenium.webdriver.support.ui import WebDriverWait # 等待元素加载的
- from selenium.webdriver.common.action_chains import ActionChains #拖拽
- from selenium.webdriver.support import expected_conditions as EC
- from selenium.common.exceptions import TimeoutException, NoSuchElementException
- from selenium.webdriver.common.by import By
- from PIL import Image
- import requests
- import time
- import re
- import random
- from io import BytesIO
-
-
- def merge_image(image_file,location_list):
- """
- 拼接图片
- :param image_file:
- :param location_list:
- :return:
- """
- im = Image.open(image_file)
- im.save('code.jpg')
- new_im = Image.new('RGB',(260,116))
- # 把无序的图片 切成52张小图片
- im_list_upper = []
- im_list_down = []
- # print(location_list)
- for location in location_list:
- # print(location['y'])
- if location['y'] == -58: # 上半边
- im_list_upper.append(im.crop((abs(location['x']),58,abs(location['x'])+10,116)))
- if location['y'] == 0: # 下半边
- im_list_down.append(im.crop((abs(location['x']),0,abs(location['x'])+10,58)))
-
- x_offset = 0
- for im in im_list_upper:
- new_im.paste(im,(x_offset,0)) # 把小图片放到 新的空白图片上
- x_offset += im.size[0]
-
- x_offset = 0
- for im in im_list_down:
- new_im.paste(im,(x_offset,58))
- x_offset += im.size[0]
- new_im.show()
- return new_im
-
- def get_image(driver,div_path):
- '''
- 下载无序的图片 然后进行拼接 获得完整的图片
- :param driver:
- :param div_path:
- :return:
- '''
- time.sleep(2)
- background_images = driver.find_elements_by_xpath(div_path)
- location_list = []
- for background_image in background_images:
- location = {}
- result = re.findall('background-image: url\("(.*?)"\); background-position: (.*?)px (.*?)px;',background_image.get_attribute('style'))
- # print(result)
- location['x'] = int(result[0][1])
- location['y'] = int(result[0][2])
-
- image_url = result[0][0]
- location_list.append(location)
-
- print('==================================')
- image_url = image_url.replace('webp','jpg')
- # '替换url http://static.geetest.com/pictures/gt/579066de6/579066de6.webp'
- image_result = requests.get(image_url).content
- # with open('1.jpg','wb') as f:
- # f.write(image_result)
- image_file = BytesIO(image_result) # 是一张无序的图片
- image = merge_image(image_file,location_list)
-
- return image
-
- def get_track(distance):
- '''
- 拿到移动轨迹,模仿人的滑动行为,先匀加速后匀减速
- 匀变速运动基本公式:
- ①v=v0+at
- ②s=v0t+(1/2)at²
- ③v²-v0²=2as
-
- :param distance: 需要移动的距离
- :return: 存放每0.2秒移动的距离
- '''
- # 初速度
- v=0
- # 单位时间为0.2s来统计轨迹,轨迹即0.2内的位移
- t=0.2
- # 位移/轨迹列表,列表内的一个元素代表0.2s的位移
- tracks=[]
- # 当前的位移
- current=0
- # 到达mid值开始减速
- mid=distance * 7/8
-
- distance += 10 # 先滑过一点,最后再反着滑动回来
- # a = random.randint(1,3)
- while current < distance:
- if current < mid:
- # 加速度越小,单位时间的位移越小,模拟的轨迹就越多越详细
- a = random.randint(2,4) # 加速运动
- else:
- a = -random.randint(3,5) # 减速运动
-
- # 初速度
- v0 = v
- # 0.2秒时间内的位移
- s = v0*t+0.5*a*(t**2)
- # 当前的位置
- current += s
- # 添加到轨迹列表
- tracks.append(round(s))
-
- # 速度已经达到v,该速度作为下次的初速度
|