Python|Python 网易易盾滑块验证功能的实现
目录
- 操作环境
- 分析
- 解决方案
- 使用selenium请求url,并触发滑块验证
- 获取验证图片并计算滑块距离
- 生成滑动轨迹
- 滑动模块
- 效果
- 资源下载
操作环境
- win10 、 mac
- Python3.9
- selenium、PIL、numpy、scipy、matplotlib
分析 网易易盾滑块验证,就长下面这个样子
具体验证原理有兴趣的可自行查询官方文档:网易易盾开发文档
话不多少,借助之前写阿里云盾滑块和极验滑块的经验,直接上代码,详细可参考:[python3 破解 geetest(极验)的滑块验证码功能]极验滑块验证
解决方案 【Python|Python 网易易盾滑块验证功能的实现】
使用selenium请求url,并触发滑块验证
def open(self):# 初始化浏览器wait = WebDriverWait(self.driver, 5)# 点击对应标签self.driver.get(cfg.TEST_URL)button = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, cfg.HD_SELECOTR)))button.click()self.tc_item = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, cfg.TC_SELECOTR)))self.tc_item.click()# 得到背景和滑块的item, 以及滑动按钮time.sleep(2)self.background_item = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, cfg.BG_SELECOTR)))self.slider_item = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, cfg.HK_SELECOTR)))self.slider_btn = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, cfg.HD_BTN)))self.offset = cfg.offsetself.background_path = cfg.background_pathself.slider_path = cfg.slider_path
获取验证图片并计算滑块距离
def get_images(self):"""获取验证码图片:return: 图片的location信息"""url = selenium_item.get_attribute("src")if url is not None:response = requests.get(url)with open(path, "wb") as f:f.write(response.content)img = Image.open(path).resize(size)img.save(path)else:class_name = selenium_item.get_attribute("class")js_cmd = ('return document.getElementsByClassName("%s")[0].toDataURL("image/png"); '% class_name)im_info = self.driver.execute_script(js_cmd)im_base64 = im_info.split(",")[1] im_bytes = base64.b64decode(im_base64)with open(path, "wb") as f:f.write(im_bytes)img = Image.open(path).resize(size)img.save(path)def compute_gap(self, array):"""计算缺口偏移"""grad = np.array(array > 0)h, w = grad.shape# img_show(grad)rows_sum = np.sum(grad, axis=1)cols_sum = np.sum(grad, axis=0)left, top, bottom = 0, 0, h# get the top indexp = np.max(rows_sum) * 0.5for i in range(h):if rows_sum[i] > p:top = ibreakfor i in range(h - 1, -1, -1):if rows_sum[i] > p:bottom = ibreakp = np.max(cols_sum) * 0.5for i in range(w):if cols_sum[i] > p:left = ibreakreturn top, bottom + 1, left
生成滑动轨迹
def get_tracks(distance):v = random.randint(0, 2)t = 1tracks = []cur = 0mid = distance * 0.8while cur < distance:if cur < mid:a = random.randint(2, 4)else:a = -random.randint(3, 5)s = v * t + 0.5 * a * t ** 2cur += sv = v + a * ttracks.append(round(s))tracks.append(distance - sum(tracks))return tracks
滑动模块
def move_to_gap(self, track):"""滑动滑块"""print('第一步,点击滑动按钮')slider = self.wait.until(EC.presence_of_element_located((By.CLASS_NAME, 'geetest_slider_button')))ActionChains(self.driver).click_and_hold(slider).perform()time.sleep(1)print('第二步,拖动元素')for track in track:ActionChains(self.driver).move_by_offset(xoffset=track, yoffset=0).perform()# 鼠标移动到距离当前位置(x,y)time.sleep(0.0001)
效果
文章图片
资源下载 https://download.csdn.net/download/qq_38154948/85343666
到此这篇关于Python 网易易盾滑块验证功能的实现的文章就介绍到这了,更多相关Python 易盾滑块验证内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
推荐阅读
- 一文搞懂python可迭代对象|一文搞懂python可迭代对象,迭代器,生成器,协程
- Python|Python 什么是flask框架(快速入门)
- 网易云易盾中标浙报反作弊服务 助力浙江新闻App健康发展
- python|Kafka
- 10个超级实用的Python自动化脚本
- 7个超好用的Python开发工具!
- Python中最常见的10种排序算法!
- python|Python学习笔记(1)---B站黑马程序员
- python|Python学习笔记(2)---B站黑马程序员
- Python|Python学习日记(初级篇02面向对象之基础)——黑马程序员视频学习笔记