准备工作
确保你已经安装了Python和OpenCV库,并且能够访问网络。
实现步骤
1. 初始化
首先,创建一个Python文件,并导入所需的库。
python
import time
import requests
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from PIL import Image
from io import BytesIO
2. 模拟登录填写,点开滑块验证
在模拟登录过程中,我们需要输入账户和密码,然后点击滑块验证按钮。
python
class CrackGeetest():
def __init__(self):
self.url = 'https://account.geetest.com/login'
self.browser = webdriver.Chrome()
self.wait = WebDriverWait(self.browser, 20)
def open(self):
self.browser.get(self.url)
email = self.wait.until(EC.presence_of_element_located((By.ID, 'email')))
password = self.wait.until(EC.presence_of_element_located((By.ID, 'password')))
email.send_keys('your_email')
password.send_keys('your_password')
button = self.wait.until(EC.element_to_be_clickable((By.CLASS_NAME, 'geetest_radar_tip')))
button.click()
3. 获取并储存有无缺口的两张图片
接下来,我们需要获取网页截图并裁剪出验证码图片,包括有缺口和无缺口的图片。
python
def get_geetest_image(self, name='captcha.png'):
top, bottom, left, right = self.get_position()
print('验证码位置', top, bottom, left, right)
screenshot = self.get_screenshot()
captcha = screenshot.crop((left, top, right, bottom))
captcha.save(name)
return captcha
def crack(self):
self.open()
image1 = self.get_geetest_image('captcha1.png')
time.sleep(2)
slider = self.get_slider()
slider.click()
image2 = self.get_geetest_image('captcha2.png')
4. 获取缺口位置
通过对比两张图片的像素点,找到缺口位置。
python
def get_gap(self, image1, image2):
left = 65
for i in range(left, image1.size[0]):
for j in range(image1.size[1]):
if not self.is_pixel_equal(image1, image2, i, j):
left = i
return left
return left
def is_pixel_equal(self, image1, image2, x, y):
pixel1 = image1.load()[x, y]
pixel2 = image2.load()[x, y]
threshold = 60
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
5. 获取移动轨迹
根据缺口位置计算出拖动滑块的轨迹。
python
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
6. 按照轨迹拖动,完成验证
最后,根据计算出的轨迹拖动滑块完成验证。
python
def move_to_gap(self, slider, track):
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()
def crack(self):
# 获取验证码图片
image1 = self.get_geetest_image('captcha1.png')
# 点按呼出缺口
slider = self.get_slider()
slider.click()
# 获取带缺口的验证码图片
image2 = self.get_geetest_image('captcha2.png')
# 获取缺口位置
gap = self.get_gap(image1, image2)
print('缺口位置', gap)
# 获取移动轨迹
track = self.get_track(gap)
print('滑动轨迹', track)
# 拖动滑块
self.move_to_gap(slider, track)
7. 完成登录
如果验证成功,则执行登录操作。
python
def login(self):
submit = self.wait.until(EC.element_to_be_clickable((By.CLASS_NAME, 'login-btn')))
submit.click()
time.sleep(10)
print('登录成功')
def crack(self):
# 模拟登录填写...
# 获取并储存有无缺口的两张图片...
# 获取缺口位置...
# 获取移动轨迹...
# 按照轨迹拖动...
# 失败后重试
if not success:
self.crack()
else:
self.login()
更多内容联系1436423940