现在极验验证码广泛应用于直播视频、金融服务、电子商务、游戏娱乐、政府企业等各大类型网站
对于这类验证,如果我们直接模拟表单请求,繁琐的认证参数与认证流程会让你蛋碎一地,我们可以用selenium驱动浏览器来解决这个问题,大致分为以下几个步骤:
1、输入用户名,密码
2、点击按钮验证,弹出没有缺口的图
3、获得没有缺口的图片
4、点击滑动按钮,弹出有缺口的图
5、获得有缺口的图片
6、对比两张图片,找出缺口,即滑动的位移
7、按照人的行为行为习惯,把总位移切成一段段小的位移
8、按照位移移动
9、完成登录
源自:
https://www.cnblogs.com/xiao-apple36/p/8878960.html
实现
位移移动需要的基础知识
位移移动相当于匀变速直线运动,类似于小汽车从起点开始运行到终点的过程
(首先为匀加速,然后再匀减速)
其中a为加速度,且为恒量(即单位时间内的加速度是不变的),t为时间
位移移动的代码实现
def get_track(distance):
'''
拿到移动轨迹,模仿人的滑动行为,先匀加速后匀减速
匀变速运动基本公式:
①v=v0+at
②s=v0t+(1/2)at²
③v²-v0²=2as
:param distance: 需要移动的距离
:return: 存放每0.2秒移动的距离
'''
v=0
t=0.1
tracks=[]
current=0
mid=distance * 4/5
distance += 10
while current if current
a = 2
else:
a = -3
v0 = v
s = v0*t+0.5*a*(t**2)
current += s
tracks.append(round(s))
v= v0+a*t
for i in range(3):
tracks.append(-2)
for i in range(4):
tracks.append(-1)
return tracks
左右滑动可查看完整代码
对比两张图片,找出缺口
def get_distance(image1,image2):
'''
拿到滑动验证码需要移动的距离
:param image1:没有缺口的图片对象
:param image2:带缺口的图片对象
:return:需要移动的距离
'''
threshold = 50
for i in range(0,image1.size[0]):
for j in range(0,image1.size[1]):
pixel1 = image1.getpixel((i,j))
pixel2 = image2.getpixel((i,j))
res_R = abs(pixel1[0]-pixel2[0])
res_G = abs(pixel1[1] - pixel2[1])
res_B = abs(pixel1[2] - pixel2[2])
if res_R > threshold and res_G > threshold and res_B > threshold:
return i
获得图片
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))
im_list_upper = []
im_list_down = []
for location in location_list:
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'))
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')
image_result = requests.get(image_url).content
image_file = BytesIO(image_result)
image = merge_image(image_file,location_list)
return image
按照位移移动
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()
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()
详细代码
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))
im_list_upper = []
im_list_down = []
for location in location_list:
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'))
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')
image_result = requests.get(image_url).content
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
t=0.2
tracks=[]
current=0
mid=distance * 7/8
distance += 10
while
current if current
a = random.randint(2,4)
else:
a = -random.randint(3,5)
v0 = v
s = v0*t+0.5*a*(t**2)
current += s
tracks.append(round(s))
v= v0+a*t
for i in range(4):
tracks.append(-random.randint(2,3))
for i in range(4):
tracks.append(-random.randint(1,3))
return tracks
def get_distance(image1,image2):
'''
拿到滑动验证码需要移动的距离
:param image1:没有缺口的图片对象
:param image2:带缺口的图片对象
:return:需要移动的距离
'''
threshold = 50
for i in range(0,image1.size[0]):
for j in range(0,image1.size[1]):
pixel1 = image1.getpixel((i,j))
pixel2 = image2.getpixel((i,j))
res_R = abs(pixel1[0]-pixel2[0])
res_G = abs(pixel1[1] - pixel2[1])
res_B = abs(pixel1[2
] - pixel2[2])
if res_R > threshold and res_G > threshold and res_B > threshold:
return i
def main_check_code(driver, element):
"""
拖动识别验证码
:param driver:
:param element:
:return:
"""
image1 = get_image(driver, '//div[@class="gt_cut_bg gt_show"]/div')
image2 = get_image(driver, '//div[@class="gt_cut_fullbg gt_show"]/div')
l = get_distance(image1, image2)
print('l=',l)
track_list = get_track(l)
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()
time.sleep(0.002)
ActionChains(driver).move_by_offset(xoffset=-random.randint(2,5), yoffset=0).perform()
time.sleep(1)
print('第三步,释放鼠标')
ActionChains(driver).release(on_element=element).perform()
time.sleep(5)
def main_check_slider(driver):
"""
检查滑动按钮是否加载
:param driver:
:return:
"""
while True:
try :
driver.get('http://www.cnbaowen.net/api/geetest/')
element = WebDriverWait(driver, 30, 0.5).until(EC.element_to_be_clickable((By.CLASS_NAME, 'gt_slider_knob')))
if element:
return element
except TimeoutException as e:
print('超时错误,继续')
time.sleep(5)
if __name__ == '__main__':
try:
count = 6
driver = webdriver.Chrome()
element = main_check_slider(driver)
while count > 0:
main_check_code(driver,element)
time.sleep(2)
try:
success_element = (By.CSS_SELECTOR, '.gt_holder .gt_ajax_tip.gt_success')
print('suc=',driver.find_element_by_css_selector('.gt_holder .gt_ajax_tip.gt_success'))
success_images = WebDriverWait(driver, 20).until(EC.presence_of_element_located(success_element))
if success_images:
print('成功识别!!!!!!')
count = 0
break
except NoSuchElementException as e:
print('识别错误,继续')
count -= 1
time.sleep(2)
else:
print('too many attempt check code ')
exit('退出程序')
finally:
driver.close()