在网络世界中,极验滑动验证码是常见的人机验证形式之一,通过拖动滑块拼合图像来完成验证。本文将介绍如何使用Python和Selenium库来实现极验滑动验证码的自动识别和验证过程。
准备工作 在开始之前,请确保已经安装好了Selenium库和Chrome浏览器,并且下载了对应版本的ChromeDriver驱动程序。
了解极验滑动验证码 极验滑动验证码的验证方式是通过拖动滑块将图像拼合完成,验证成功后才能继续操作。我们将以极验滑动验证码官网提供的示例为基础进行演示。
实现步骤
- 初始化 首先,我们需要初始化链接地址、创建模拟浏览器对象,并设置登录账户和密码等信息。
python
EMAIL = 'your_email@example.com' PASSWORD = 'your_password'
class CrackGeetest(): def init(self): self.url = 'https://account.geetest.com/login' self.browser = webdriver.Chrome() self.wait = WebDriverWait(self.browser, 20) self.email = EMAIL self.password = PASSWORD
def crack(self):
pass
主程序入口
if name == 'main': crack = CrackGeetest() crack.crack() 2. 模拟登录填写,打开滑块验证 在实例化CrackGeetest对象后调用crack()方法开始模拟登录验证。首先打开登录界面,输入账户和密码,然后点击滑动验证按钮。
python
class CrackGeetest(): # ...
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(self.email)
password.send_keys(self.password)
def get_geetest_button(self):
button = self.wait.until(EC.element_to_be_clickable((By.CLASS_NAME, 'geetest_radar_tip')))
return button
def crack(self):
# 输入用户名密码
self.open()
# 点击验证按钮
button = self.get_geetest_button()
button.click()
# ...
- 获取并储存有无缺口的两张图片 获取无缺口的验证图片并保存到本地,然后点击滑块,获取有缺口的验证图片并保存。
python
class CrackGeetest(): # ...
def get_position(self):
img = self.wait.until(EC.presence_of_element_located((By.CLASS_NAME, 'geetest_canvas_img')))
time.sleep(2)
location = img.location
size = img.size
top, bottom, left, right = location['y'], location['y'] + size['height'], location['x'], location['x'] + size['width']
return (top, bottom, left, right)
def get_screenshot(self):
screenshot = self.browser.get_screenshot_as_png()
screenshot = Image.open(BytesIO(screenshot))
return screenshot
def get_geetest_image(self, name='captcha.png'):
top, bottom, left, right = self.get_position()
screenshot = self.get_screenshot()
captcha = screenshot.crop((left, top, right, bottom))
captcha.save(name)
return captcha
def get_slider(self):
slider = self.wait.until(EC.element_to_be_clickable((By.CLASS_NAME, 'geetest_slider_button')))
return slider
def crack(self):
# ...
# 获取验证码图片
image1 = self.get_geetest_image('captcha1.png')
# 点击呼出缺口
slider = self.get_slider()
slider.click()
# 获取带缺口的验证码图片
image2 = self.get_geetest_image('captcha2.png')
# ...
- 获取缺口位置 对比两张图片的像素点,得到缺口位置。
python
class CrackGeetest(): # ...
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
def crack(self):
# ...
# 获取缺口位置
gap = self.get_gap(image1, image2)
print('缺口位置', gap)
# 减去缺口位移
gap -= BORDER
# ...
- 获取移动轨迹 根据缺口位置获取滑块的移动轨迹。
python
class CrackGeetest(): # ...
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
def crack(self):
# ...
# 获取移动轨迹
track = self.get_track(gap)
print('滑动轨迹', track)
# ...
- 按照轨迹拖动,完成验证 根据移动轨迹拖动滑块完成验证。
python
class CrackGeetest(): # ...
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
更多内容联系1436423940