python3----练习题(过滑块验证)

  1 # 导入模块
  2 from selenium import webdriver
  3 from selenium.webdriver import ActionChains
  4 from selenium.webdriver.common.by import By
  5 from selenium.webdriver.common.keys import Keys
  6 from selenium.webdriver.support import expected_conditions as EC
  7 from selenium.webdriver.support.wait import WebDriverWait
  8 from PIL import Image
  9 import time
 10
 11 def get_snap(driver):
 12     driver.save_screenshot(‘full_snap.png‘)
 13     page_snap_obj = Image.open(‘full_snap.png‘)
 14     return page_snap_obj
 15
 16 def get_image(driver):
 17     img = driver.find_element_by_class_name(‘geetest_canvas_img‘)
 18     time.sleep(2)
 19     location = img.location
 20     size = img.size
 21     print(size)
 22
 23     left = location[‘x‘]
 24     top = location[‘y‘]
 25     right = left+size[‘width‘]
 26     bottom = top+size[‘height‘]
 27
 28     page_snap_obj = get_snap(driver)
 29     image_obj = page_snap_obj.crop((left, top, right, bottom))
 30     # image_obj.show()
 31     return image_obj
 32
 33 def get_distance(image1, image2):
 34     start = 57
 35     threhold = 60
 36
 37     for i in range(start, image1.size[0]):
 38         for j in range(image1.size[1]):
 39             rgb1 = image1.load()[i, j]
 40             rgb2 = image2.load()[i, j]
 41             res1 = abs(rgb1[0]-rgb2[0])
 42             res2 = abs(rgb1[1]-rgb2[1])
 43             res3 = abs(rgb1[2]-rgb2[2])
 44             # print(res1,res2,res3)
 45             if not (res1 <threhold and res2 <threhold and res3 < threhold):
 46                 return i - 7
 47     return i - 7
 48
 49 def get_tracks(distance):
 50     distance += 20  # 先滑过一点, 最后再反着滑动回来
 51     v = 0
 52     t = 0.2
 53     forward_tracks = []
 54
 55     current = 0
 56     mid = distance * 3 / 5
 57     while current < distance:
 58         if current < mid:
 59             a = 2
 60         else:
 61             a = -3
 62
 63         s = v * t + 0.5 * a * (t**2)
 64         v = v + a * t
 65         current += s
 66         forward_tracks.append(round(s))
 67
 68     # 反着滑动到准确位置
 69     back_tracks = [-1, -1, -1, -2, -3, -2, -2, -2, -2, -1, -1, -1]  # 总共等于 -20
 70
 71     return {‘forward_tracks‘:forward_tracks, ‘back_tracks‘:back_tracks}
 72
 73 def crack(driver):  # 破解滑动认证
 74     # 1.点击按钮,得到没有缺口的图片
 75     button = driver.find_element_by_class_name(‘geetest_radar_tip‘)
 76     button.click()
 77
 78     # 2.获取没有缺口的图片
 79     image1 = get_image(driver)
 80
 81     # 3.点击滑动按钮,得到有缺口的图片
 82     button = driver.find_element_by_class_name(‘geetest_slider_button‘)
 83     button.click()
 84
 85     # 4.获取有缺口的图片
 86     image2 = get_image(driver)
 87
 88     # 5.对比两种图片的像素点,找出位移
 89     distance = get_distance(image1, image2)
 90
 91     # 6.模拟人的行为习惯,根据总位移得到的行为轨迹
 92     tracks = get_tracks(distance)
 93     print(tracks)
 94
 95     # 7.按照人行动轨迹先正向滑动,后反向滑动
 96     button = driver.find_element_by_class_name(‘geetest_slider_button‘)
 97     ActionChains(driver).click_and_hold(button).perform()
 98
 99     # 正常人类总是自信满满地开始正向滑动,自信的表现是疯狂加速
100     for track in tracks[‘forward_tracks‘]:
101         ActionChains(driver).move_by_offset(xoffset=track, yoffset=0).perform()
102
103     # 结果傻逼了,正常的人类停顿了一下,回过神来发现,卧槽,滑过了,然后开始反向滑动
104     time.sleep(0.3)
105     for back_track in tracks[‘back_tracks‘]:
106         ActionChains(driver).move_by_offset(xoffset=back_track, yoffset=0).perform()
107
108     # 小范围震荡一下, 进一步迷惑极验后台, 这一步可以极大的提高成功率
109     time.sleep(0.1)
110     ActionChains(driver).move_by_offset(xoffset=-3, yoffset=0).perform()
111     ActionChains(driver).move_by_offset(xoffset=3, yoffset=0).perform()
112
113     # 成功后,骚包人类总喜欢默默地欣赏一下自己拼图的成果,然后恋恋不舍地松开那只脏手
114     time.sleep(0.5)
115     ActionChains(driver).release().perform()
116
117 def login_cnblogs(username,password):
118     driver = webdriver.Chrome()
119     try:
120         # 1、输入账号密码回车
121         driver.implicitly_wait(3)
122         driver.get(‘https://passport.cnblogs.com/user/signin‘)
123
124         input_username = driver.find_element_by_id(‘input1‘)
125         input_pwd = driver.find_element_by_id(‘input2‘)
126         signin = driver.find_element_by_id(‘signin‘)
127
128         input_username.send_keys(username)
129         input_pwd.send_keys(password)
130         signin.click()
131
132         # 2、破解滑动认证
133         crack(driver)
134
135         time.sleep(10)  # 睡时间长一点,确定登录成功
136     finally:
137         driver.close()
138
139
140
141 if __name__ == ‘__main__‘:
142     login_cnblogs(username=‘aaaaa‘,password=‘aaaaa‘)

原文地址:https://www.cnblogs.com/jonm/p/8313490.html

时间: 2024-11-02 23:08:14

python3----练习题(过滑块验证)的相关文章

原生js实现拖动滑块验证

拖动滑块验证是现在的网站随处可见的,各式各样的拖动法都有. 下面实现的是某宝的拖动滑块验证: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style type="text/css"> .drag{position: relative;

JS拖动滑块验证

使用这种验证方法的目的:证明当前的用户不是机器人~防止恶意操作. 实现思路: 1.获取silde滑块(获取元素) 2.为元素注册事件———鼠标点击事件(onmousedown)鼠标点击之后获得当前鼠标的X坐标. 3.如何获取到鼠标的x坐标——使用clientX事件(当事件被触发时,鼠标指针的水平坐标). 4.鼠标移动事件发生后根据从最开始点击的X值到移动后的X值之差,作为滑块移动的差值———— 鼠标移动事件 (onmousemove): 5.获取鼠标移动之后的X坐标 6.获得初始X坐标和移动后X

Python3练习题 001:4个数字求不重复的3位数

#Python练习题 001:4个数字求不重复的3位数#方法一import itertoolsres = [][res.append(i[0]*100 + i[1]*10 + i[2]) for i in itertools.permutations(range(1,5),3)]print(res, end = ',') """参考https://www.cnblogs.com/iderek/p/5952126.html""" #方法二for i

【Python3练习题 025】 一个数,判断它是不是回文数。即12321是回文数,个位与万位相同,十位与千位相同

[Python练习题 025] 一个5位数,判断它是不是回文数.即12321是回文数,个位与万位相同,十位与千位相同 x = input('请输入任意位数的数字:') if x == x[::-1]:     print('%s是个回文数' % x) else:     print('%s不是回文数' % x)   原文地址:https://www.cnblogs.com/jackzz/p/9125539.html

Python3练习题系列(06)——各种符号总结

Python3中的各种符号总结 1关键字 import keyword print(keyword.kwlist, end='\t') ['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is',

python3练习题四

循环 题目: 答案: 1 #!/usr/bin/env python3 2 #-*- coding:utf-8 -*- 3 4 L = ['Bart', 'Lisa', 'Adam'] 5 6 for i in L: 7 print('Hello ' + i + '!') 原文地址:https://www.cnblogs.com/yrxns/p/9409705.html

Android 滑块验证

先上图看看实现效果 1.在 app 的 build.gradle 添加依赖 implementation 'com.luozm.captcha:captcha:1.1.2' 2.将 Captcha 添加至布局文件 <com.luozm.captcha.Captcha android:id="@+id/captCha" android:layout_centerInParent="true" android:layout_width="match_pa

python3 练习题100例 (三)

题目三:一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少? #!/usr/bin/env python3 # -*- coding: utf-8 -*- """ 题目三:一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?""" __author__ = 'Fan Lijun' import math for x in range(10000): if math.sqr

python3 练习题100例 (八)

题目八:暂停一秒输出,并格式化当前时间. #!/usr/bin/env python3 # -*- coding: utf-8 -*- """ 题目八:暂停一秒输出,并格式化当前时间.""" __author__ = 'Fan Lijun' import time print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))) # 暂停一秒 time.sleep(1)

python3 练习题100例 (十六)鸡尾酒疗法

#!/usr/bin/env python3 # -*- coding: utf-8 -*- __author__ = 'Fan Lijun' n = input('请输入一个大于1,小于等于20的整数:') lst = [] for i in range(int(n)): z = eval(input('请输入总病例数:')) lst.append(z) y = eval(input('请输入有效病历数:')) lst.append(y) j = lst[1] / lst[0] num = 1