今日内容 :
破解极验滑动验证
'''
破解极验滑动验证
破解极验滑动验证
博客园登录url:
https://account.cnblogs.com/signin?returnUrl=https%3A%2F%2Fwww.cnblogs.com%2F
代码逻辑:
1、输入用户名与密码,并点击登录
2、弹出滑动验证,获取有缺口与完整的图片
3、通过像素点进行比对,获取滑动位移距离
4、模拟人的行为轨迹
5、开始滑动
'''
from selenium import webdriver
from selenium.webdriver import ActionChains
from PIL import Image
import time
import random
def main():
driver = webdriver.Chrome(r'/Users/nadia/Downloads/chromedriver')
# 输入用户名和密码并点击登录
driver.get("https://account.cnblogs.com/signin?returnUrl=https%3A%2F%2Fwww.cnblogs.com%2F")
user_input = driver.find_element_by_id('LoginName')
user_input.send_keys('xxxx') # xxxx指代你的用户名
time.sleep(2)
pwd_input = driver.find_element_by_id('Password')
pwd_input.send_keys('xxxxxxxx.')# xxxx指代你的密码
login_submit = driver.find_element_by_id('submitBtn')
login_submit.click()
# 获取完整的图片
img1 = get_image1(driver)
with open('img1.png','wb') as f:
f.write(img1)
# 获取有缺口的图片
img2 = get_image2(driver)
with open('img2.png','wb') as f:
f.write(img2)
# 获取滑动距离
distance = get_distance(img1,img2)
print(distance)
# 模拟人的滑动轨迹
get_track_move()
time.sleep(100)
move_list = get_track_move(distance)['move_list']
back_list = get_track_move(distance)['back_list']
# 开始滑动
move_tag = driver.find_element_by_class_name('geetest_slider_button')
# 点击恩柱滑动按钮
ActionChains(driver).click_and_hold(move_tag).perform()
# 滑动
for move in move_list:
ActionChains(driver).move_by_offset(xoffset=move,yoffset=0).perform()
time.sleep(0.1)
# 后滑
for back in back_list :
ActionChains(driver).move_by_offset(xoffset=-5,yoffset=0)
ActionChains(driver).release()
#模拟人的滑动轨迹
def get_track_move(distance):
distance += 20
'''
v= v0 + a* t
s= v0 *t + 0.5*a*(t**2)
'''
v0 = 0
#时间
t = 0.2
#位置
s =0
# 滑动轨迹列表
move_list = []
# 中间值
mid = distance /5 *3
# 加减速度列表
a_list = [1,2,3,4]
# 后退列表,自定义后退滑动轨迹
back_list = [1,1,2,3,1,5,4,3]
# 循环位移
while s<distance:
if s<mid:
a = a_list[random.randint(0,len(a_list) - 1)]
else:
a = - a_list[random.randint(0,len(a_list) - 1)]
s1 = round(v * t + 0.5 * a * (t**2))#取整
s += s1
v= v0 + a* t
v0 = v
move_list.append(s)
return {'move_list':move_list,'back_list':back_list}
# 获取滑块滑动距离
def get_distance(img1,img2):
# 滑块距离
start = 60
# 像素差
num = 60
# size用元祖表示,用索引取得img的size width 和height
for x in range(start,img1.size[0]):
for y in range(img1.size[1]):
# 获取两图片每个坐标的像素点
rgb1 = img1.load()[x,y]
rgb2 = img2.load()[x,y]
# 获取像素点各个参数差值
r = abs(rgb1[0] - rgb2[0])
g = abs(rgb1[1] - rgb2[1])
b = abs(rgb1[2] - rgb2[2])
# 如果条件成立找到缺口位置
if not (r < 60 and g < 60 and b < 60):
return x - 7 #误差7
def cut_image(driver):
# 获取整个页面的图片,名字为:snap.jpg
driver.save_screenshot('snap.png')
# 获取滑动 小图片
image = driver.find_element_by_class_name('geetest_canvas_img')
print(image.location)
print(image.size)
#获取 小图片 四个边角的位置
left = image.location['x']
top = image.location['y']
right = left + image.size['width']
buttom = top + image.size['height']
# 调用open方法打开全局图片
image_obj = Image.open('snap.png')
# 通过image_obj对象对小图片进行截取
# box: The crop rectangle, as a (left, upper, right, lower)-tuple.
img = image_obj.crop((left, top, right, buttom))
# 打开截取后的小图片
img.show()
return img
def get_image1(driver):
time.sleep(2)
# 修改document文档数,把完整图片的display属性修改为block
js_code = 'var x = document.getElementsByClassName("geetest_canvas_fullbg")[0].style.display = "block";'
# 执行jscode
driver.execute_script(js_code)
# 截取图片
image = cut_image(driver)
return image
def get_image2(driver):
time.sleep(2)
# 修改document文档树,把完整图片的display属性修改为block
js_code = '''
var x = document.getElementsByClassName("geetest_canvas_fullbg")[0].style.display = "none";
'''
# 执行js代码
driver.execute_script(js_code)
# 截取图片
image = cut_image(driver)
return image
if __name__ == "__main__":
main()