• 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')
  • 相关阅读:
    搭建AngualarJS开发环境
    博客从博客园迁移到github上
    探讨Android中的内置浏览器和Chrome
    Netbeans配置Xdebug
    关于 Xcode 调试工具 GDB and LLDB
    关于ios 运行时 介绍的比较详细的帖子
    你不知道的KVO的内部实现
    sqlite 判断表中是否包含 某个字段
    发一些靠谱的招聘网站(含ios)
    解析 友盟崩溃信息
  • 原文地址:https://www.cnblogs.com/jonm/p/8313490.html
Copyright © 2020-2023  润新知