实现步骤
1. 初始化
首先,导入必要的库并创建一个浏览器实例。
python
from selenium import webdriver
import time
class CrackGeetest():
def __init__(self):
self.browser = webdriver.Chrome()
# 其他操作...
2. 模拟登录填写,点开滑块验证
在模拟登录过程中,我们需要输入账户和密码,然后点击滑块验证按钮。
python
class CrackGeetest():
def __init__(self):
self.browser = webdriver.Chrome()
def login(self):
self.browser.get("https://account.geetest.com/login")
email = self.browser.find_element_by_id("email")
email.send_keys("你的登录账户")
password = self.browser.find_element_by_id("password")
password.send_keys("你的登录密码")
button = self.browser.find_element_by_class_name("geetest_radar_tip")
button.click()
# 其他操作...
if __name__ == "__main__":
crack = CrackGeetest()
crack.login()
# 其他操作...
3. 获取并储存有无缺口的两张图片
接下来,我们需要获取网页截图并裁剪出验证码图片,包括有缺口和无缺口的图片。
python
from PIL import Image
from io import BytesIO
class CrackGeetest():
def __init__(self):
self.browser = webdriver.Chrome()
def get_captcha_images(self):
self.browser.save_screenshot("screenshot.png")
img = self.browser.find_element_by_class_name("geetest_canvas_img")
location = img.location
size = img.size
left = location['x']
top = location['y']
right = left + size['width']
bottom = top + size['height']
screenshot = Image.open("screenshot.png")
captcha1 = screenshot.crop((left, top, right, bottom))
captcha1.save("captcha1.png")
slider = self.browser.find_element_by_class_name("geetest_slider_button")
slider.click()
time.sleep(2) # 等待滑块滑动完成
self.browser.save_screenshot("screenshot.png")
screenshot = Image.open("screenshot.png")
captcha2 = screenshot.crop((left, top, right, bottom))
captcha2.save("captcha2.png")
# 其他操作...
if __name__ == "__main__":
crack = CrackGeetest()
crack.login()
crack.get_captcha_images()
# 其他操作...
4. 获取缺口位置
通过对比两张图片的像素点,找到缺口位置。
python
import numpy as np
class CrackGeetest():
def __init__(self):
self.browser = webdriver.Chrome()
def get_gap(self, image1, image2):
left = 60
for i in range(left, image1.size[0]):
for j in range(image1.size[1]):
pixel1 = image1.load()[i, j]
pixel2 = image2.load()[i, j]
threshold = 60
if not self.is_pixel_equal(pixel1, pixel2, threshold):
left = i
return left
return left
def is_pixel_equal(self, pixel1, pixel2, threshold):
if abs(pixel1[0] - pixel2[0]) < threshold and abs(pixel1[1] - pixel2[1]) < threshold and abs(
pixel1[2] - pixel2[2]) < threshold:
return True
else:
return False
def get_gap_position(self):
image1 = Image.open("captcha1.png")
image2 = Image.open("captcha2.png")
gap = self.get_gap(image1, image2)
print("缺口位置:", gap)
return gap
# 其他操作...
if __name__ == "__main__":
crack = CrackGeetest()
crack.login()
crack.get_captcha_images()
crack.get_gap_position()
# 其他操作...
5. 获取移动轨迹
根据缺口位置计算出拖动滑块的轨迹。
python
class CrackGeetest():
def __init__(self):
self.browser = webdriver.Chrome()
def get_track(self, distance):
track = []
current = 0
mid = distance * 4 / 5
t = 0.2
v = 0
while current < distance:
if current < mid:
a = 2
else:
a = -3
v0 = v
v = v0 + a * t
move = v0 * t + 1 / 2 * a * t * t
current += move
track.append(round(move))
return track
# 其他操作...
if __name__ == "__main__":
crack = CrackGeetest()
crack.login()
crack.get_captcha_images()
gap = crack.get_gap_position()
track = crack.get_track(gap)
# 其他操作...
6. 按照轨迹拖动,完成验证
最后,根据计算出的轨迹拖动滑块完成验证。
python
class CrackGeetest():
def __init__(self):
self.browser = webdriver.Chrome()
def move_to_gap(self, track):
slider = self.browser.find_element_by_class_name("geetest_slider_button")
ActionChains(self.browser).click_and_hold(slider).perform()
for x in track:
ActionChains(self.browser).move_by_offset(xoffset=x, yoffset=0).perform()
time.sleep(0.5)
ActionChains(self.browser).release().perform()
# 其他操作...
if __name__ == "__main__":
crack = CrackGeetest()
crack.login()
crack.get_captcha_images()
gap = crack.get_gap_position()
track = crack.get_track(gap)
crack.move_to_gap(track)
# 其他操作...
7. 完成登录
如果验证成功,则执行登录操作。
python
class CrackGeetest():
def __init__(self):
self.browser = webdriver.Chrome()
def login(self):
# 登录操作...
success = self.wait.until(
EC.text_to_be_present_in_element((By.CLASS_NAME, 'geetest_success_radar_tip_content'), '验证成功'))
if success:
print("登录成功")
else:
print("登录失败")
# 其他操作...
if __name__ == "__main__":
crack = CrackGeetest()
crack.login()
crack.get_captcha_images()
gap = crack.get_gap_position()
track = crack.get_track(gap)
crack.move_to_gap(track)
crack.login()
更多内容联系1436423940