当前位置:   article > 正文

破解geetest网站的滑动验证码_crack = crackgeetest() crack.crack()

crack = crackgeetest() crack.crack()
  1. from selenium import webdriver
  2. from selenium.webdriver.support.wait import WebDriverWait
  3. from selenium.webdriver.support import expected_conditions as EC
  4. from selenium.webdriver.common.by import By
  5. from PIL import Image
  6. from io import BytesIO
  7. from selenium.webdriver import ActionChains
  8. import time
  9. EMAIL = "**************"
  10. PASSWORD = "********"
  11. BORDER = 6
  12. class CrackGeetest():
  13. def __init__(self):
  14. self.url = "https://account.geetest.com/login"
  15. self.email = EMAIL
  16. self.password = PASSWORD
  17. self.browser = webdriver.Chrome()
  18. self.wait = WebDriverWait(self.browser,10)
  19. def get_geetest_button(self):
  20. button = self.wait.until(EC.element_to_be_clickable((By.CLASS_NAME, "geetest_radar_tip")))
  21. return button
  22. def get_position(self):
  23. img = self.wait.until(EC.presence_of_element_located((By.CLASS_NAME,"geetest_canvas_img ")))
  24. time.sleep(2)
  25. location = img.location
  26. size = img.size
  27. top, bottom, left, right = location["y"], location["y"] + size["height"], location["x"],\
  28. location["x"] + size["width"]
  29. return (top, bottom, left, right)
  30. def get_geetest_image(self, name = "captcha.png"):
  31. top, bottom, left, right = self.get_position()
  32. print("验证码位置", top, bottom, left, right)
  33. screenshot = self.get_screenshot()
  34. captcha = screenshot.crop((left, top, right, bottom)) #crop方法是Image里的截图方法 先后顺序有规定(左,上,右,下)
  35. captcha.save(name)
  36. return captcha
  37. def get_screenshot(self): #截图功能
  38. screenshot = self.browser.get_screenshot_as_png()
  39. screenshot = Image.open(BytesIO(screenshot))
  40. return screenshot
  41. def get_slider(self):
  42. slider = self.wait.until(EC.element_to_be_clickable((By.CLASS_NAME,"geetest_slider_button")))
  43. return slider
  44. def open(self):
  45. self.browser.get(self.url)
  46. email = self.wait.until(EC.presence_of_element_located((By.NAME, "email")))
  47. password = self.wait.until(EC.presence_of_element_located((By.NAME, "password")))
  48. email.send_keys(self.email)
  49. password.send_keys(self.password)
  50. def is_pixel_equal(self,image1,image2,x,y):
  51. pixel1 = image1.load()[x, y]#带缺口图片
  52. pixel2 = image2.load()[x, y]#不带缺口图片
  53. threshold = 60
  54. if abs(pixel1[0] - pixel2[0]) < threshold and abs(pixel1[1] -pixel2[1]) < threshold and abs(pixel1[2] - pixel2[2]) < threshold:
  55. return True
  56. else:
  57. return False
  58. def get_gap(self,image1,image2):
  59. left = 60
  60. for i in range(left, image1.size[0]):#从x轴的60开始遍历
  61. for j in range(image1.size[1]):#y轴
  62. if not self.is_pixel_equal(image1, image2, i, j):
  63. left = i
  64. return left
  65. return left
  66. def get_track(self,distance):
  67. current = 0 #当前距离
  68. mid = distance * 4 / 5#到mid时减速
  69. V = 0 #初速度为0
  70. track = []#移动轨迹
  71. t = 0.2#时间
  72. while current < distance:
  73. if current < mid:
  74. a = 2
  75. else:
  76. a = -3
  77. V0 = V #初速度为0赋值给V0,下一循环后的初速度是上一循环时的速度
  78. V = V0 + a * t #得到加速度后的速度
  79. move = V0 * t + a * t * t / 2 #移动距离
  80. current += move #移动后的距离
  81. track.append(round(move))
  82. return track
  83. def move_to_gap(self, slider,track):
  84. ActionChains(self.browser).click_and_hold(slider).perform()
  85. for x in track:
  86. ActionChains(self.browser).move_by_offset(xoffset=x, yoffset=0).perform()
  87. time.sleep(0.5)
  88. ActionChains(self.browser).release().perform()
  89. def login(self):
  90. submit = self.wait.until(EC.element_to_be_clickable((By.CLASS_NAME,"login-btn")))
  91. submit.click()
  92. time.sleep(1)
  93. print("登陆成功!")
  94. def crack(self):
  95. self.open()
  96. button = self.get_geetest_button()
  97. button.click()
  98. image1 = self.get_geetest_image("captcha1.png")
  99. slider = self.get_slider()
  100. slider.click()
  101. image2 = self.get_geetest_image("captcha2.png")
  102. gap = self.get_gap(image1, image2)
  103. print("缺口位置", gap)
  104. gap = gap - BORDER #拼图左边和图片边缘有6的距离 所以减去6
  105. track = self.get_track(gap)
  106. print("滑动轨迹", track)
  107. self.move_to_gap(slider,track)
  108. sucess = self.wait.until(EC.text_to_be_present_in_element((By.CLASS_NAME,"geetest_success_radar_tip"),"验证成功"))
  109. print(sucess)
  110. if not sucess:
  111. self.crack()
  112. else:
  113. self.login()
  114. if __name__ == "__main__":
  115. crack = CrackGeetest()
  116. crack.crack()

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/287266
推荐阅读
相关标签
  

闽ICP备14008679号