python入门教程 - 滑块实战【附源码】

水深无声 2023-09-30 03:19 51阅读 0赞

文末源码,阅读大约2.8分钟

傻瓜式教程 - 体验滑块,提供练习场景及源码。


文章目录

  • 环境安装
  • 效果展示
  • 源码

image

环境安装

安装python需要的依赖包

cv2 安装可以参考这里:https://javapub.blog.csdn.net/article/details/123656345

安装webdriver -> chrome

下载对应版本,放在本地 D:\anaconda3\Scripts 目录下

https://registry.npmmirror.com/binary.html?path=chromedriver


效果展示

GIF效果:https://tva2.sinaimg.cn/large/007F3CC8ly1h0ku3yh9g5g31ex0pfwus.gif

动画

cv2使用参考:https://blog.csdn.net/RNG\_uzi\_/article/details/90034485

注意:测试时慢点刷,容易封IP。

源码

有问题可以留言探讨,公众号:JavaPub

对源码加了大量注释

测试网站:http://app.miit-eidc.org.cn/miitxxgk/gonggao/xxgk/queryCpParamPage?dataTag=Z&gid=U3119671&pc=303

  1. import os
  2. import cv2
  3. import time
  4. import random
  5. import requests
  6. import numpy as np
  7. from PIL import Image
  8. from io import BytesIO
  9. from selenium import webdriver
  10. from selenium.webdriver.common.by import By
  11. from selenium.webdriver import ActionChains
  12. from selenium.webdriver.support.wait import WebDriverWait
  13. from selenium.webdriver.support import expected_conditions as EC
  14. class CrackSlider():
  15. def __init__(self):
  16. # self.browser = webdriver.Edge()
  17. self.browser = webdriver.Chrome()
  18. self.s2 = r'//*[@id="captcha_div"]/div/div[1]/div/div[1]/img[1]'
  19. self.s3 = r'//*[@id="captcha_div"]/div/div[1]/div/div[1]/img[2]'
  20. self.url = 'http://app.miit-eidc.org.cn/miitxxgk/gonggao/xxgk/queryCpParamPage?dataTag=Z&gid=U3119671&pc=303' # 测试网站
  21. self.wait = WebDriverWait(self.browser, 20)
  22. self.browser.get(self.url)
  23. # 保存俩张图片
  24. def get_img(self, target, template, xp):
  25. time.sleep(3)
  26. target_link = self.browser.find_element_by_xpath(self.s2).get_attribute("src")
  27. template_link = self.browser.find_element_by_xpath(self.s3).get_attribute("src")
  28. target_img = Image.open(BytesIO(requests.get(target_link).content))
  29. template_img = Image.open(BytesIO(requests.get(template_link).content))
  30. target_img.save(target)
  31. template_img.save(template)
  32. size_loc = target_img.size
  33. print('size_loc[0]-----\n')
  34. print(size_loc[0])
  35. zoom = xp / int(size_loc[0]) # 耦合像素
  36. print('zoom-----\n')
  37. print(zoom)
  38. return zoom
  39. def change_size(self, file):
  40. image = cv2.imread(file, 1) # 读取图片 image_name应该是变量
  41. img = cv2.medianBlur(image, 5) # 中值滤波,去除黑色边际中可能含有的噪声干扰。去噪。
  42. b = cv2.threshold(img, 15, 255, cv2.THRESH_BINARY) # 调整裁剪效果,二值化处理。
  43. binary_image = b[1] # 二值图--具有三通道
  44. binary_image = cv2.cvtColor(binary_image, cv2.COLOR_BGR2GRAY)
  45. x, y = binary_image.shape
  46. edges_x = []
  47. edges_y = []
  48. for i in range(x):
  49. for j in range(y):
  50. if binary_image[i][j] == 255:
  51. edges_x.append(i)
  52. edges_y.append(j)
  53. left = min(edges_x) # 左边界
  54. right = max(edges_x) # 右边界
  55. width = right - left # 宽度
  56. bottom = min(edges_y) # 底部
  57. top = max(edges_y) # 顶部
  58. height = top - bottom # 高度
  59. pre1_picture = image[left:left + width, bottom:bottom + height] # 图片截取
  60. return pre1_picture # 返回图片数据
  61. # 匹配比对俩图距离
  62. def match(self, target, template):
  63. img_gray = cv2.imread(target, 0)
  64. img_rgb = self.change_size(template)
  65. template = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY) # 图片格式转换为灰度图片
  66. # cv2.imshow('template', template)
  67. # cv2.waitKey(0)
  68. res = cv2.matchTemplate(img_gray, template, cv2.TM_CCOEFF_NORMED) # 匹配模式,匹配图片
  69. run = 1
  70. # 使用二分法查找阈值的精确值
  71. L = 0
  72. R = 1
  73. while run < 20:
  74. run += 1
  75. threshold = (R + L) / 2
  76. if threshold < 0:
  77. print('Error')
  78. return None
  79. loc = np.where(res >= threshold)
  80. if len(loc[1]) > 1:
  81. L += (R - L) / 2
  82. elif len(loc[1]) == 1:
  83. break
  84. elif len(loc[1]) < 1:
  85. R -= (R - L) / 2
  86. res = loc[1][0]
  87. print('match distance-----\n')
  88. print(res)
  89. return res
  90. def move_to_gap(self, tracks):
  91. slider = self.wait.until(EC.element_to_be_clickable((By.CLASS_NAME, 'yidun_slider')))
  92. ActionChains(self.browser).click_and_hold(slider).perform()
  93. #element = self.browser.find_element_by_xpath(self.s3)
  94. #ActionChains(self.browser).click_and_hold(on_element=element).perform()
  95. while tracks:
  96. x = tracks.pop(0)
  97. print('tracks.pop(0)-----\n')
  98. print(x)
  99. ActionChains(self.browser).move_by_offset(xoffset=x, yoffset=0).perform()
  100. #ActionChains(self.browser).move_to_element_with_offset(to_element=element, xoffset=x, yoffset=0).perform()
  101. #time.sleep(0.01)
  102. time.sleep(0.05)
  103. ActionChains(self.browser).release().perform()
  104. def move_to_gap1(self, distance):
  105. distance += 46
  106. time.sleep(1)
  107. element = self.browser.find_element_by_xpath(self.s3)
  108. ActionChains(self.browser).click_and_hold(on_element=element).perform()
  109. ActionChains(self.browser).move_to_element_with_offset(to_element=element, xoffset=distance, yoffset=0).perform()
  110. #ActionChains(self.browser).release().perform()
  111. time.sleep(1.38)
  112. ActionChains(self.browser).release(on_element=element).perform()
  113. def move_to_gap2(self, distance):
  114. element = self.browser.find_elements_by_class_name("yidun_slider")[0]
  115. action = ActionChains(self.browser)
  116. mouse_action = action.click_and_hold(on_element=element)
  117. distance += 11
  118. distance = int(distance * 32/33)
  119. move_steps = int(distance/4)
  120. for i in range(0,move_steps):
  121. mouse_action.move_by_offset(4,random.randint(-5,5)).perform()
  122. time.sleep(0.1)
  123. mouse_action.release().perform()
  124. # 计算出先加速、后加速的数组
  125. def get_tracks(self, distance, seconds, ease_func):
  126. distance += 20
  127. tracks = [0]
  128. offsets = [0]
  129. for t in np.arange(0.0, seconds, 0.1):
  130. ease = ease_func
  131. print('ease-----\n')
  132. print(ease)
  133. offset = round(ease(t / seconds) * distance)
  134. print('offset-----\n')
  135. print(offset)
  136. tracks.append(offset - offsets[-1])
  137. print('offset - offsets[-1]-----\n')
  138. print(offset - offsets[-1])
  139. offsets.append(offset)
  140. print('offsets-----\n')
  141. print(offsets)
  142. tracks.extend([-3, -2, -3, -2, -2, -2, -2, -1, -0, -1, -1, -1])
  143. return tracks
  144. def get_tracks1(self,distance):
  145. """
  146. 根据偏移量获取移动轨迹
  147. :param distance: 偏移量
  148. :return: 移动轨迹
  149. """
  150. # 移动轨迹
  151. track = []
  152. # 当前位移
  153. current = 0
  154. # 减速阈值
  155. mid = distance * 4 / 5
  156. # 计算间隔
  157. t = 0.2
  158. # 初速度
  159. v = 0
  160. while current < distance:
  161. if current < mid:
  162. # 加速度为正 2
  163. a = 4
  164. else:
  165. # 加速度为负 3
  166. a = -3
  167. # 初速度 v0
  168. v0 = v
  169. # 当前速度 v = v0 + at
  170. v = v0 + a * t
  171. # 移动距离 x = v0t + 1/2 * a * t^2
  172. move = v0 * t + 1 / 2 * a * t * t
  173. # 当前位移
  174. current += move
  175. # 加入轨迹
  176. track.append(round(move))
  177. return track
  178. def ease_out_quart(self, x):
  179. res = 1 - pow(1 - x, 4)
  180. print('ease_out_quart-----\n')
  181. print(res)
  182. return res
  183. # 发生意外,请留言。https://javapub.blog.csdn.net/article/details/123730597
  184. if __name__ == '__main__':
  185. xp = 320 # 验证码的像素-长
  186. target = 'target.jpg' # 临时保存的图片名
  187. template = 'template.png' # 临时保存的图片名
  188. cs = CrackSlider()
  189. zoom = cs.get_img(target, template, xp)
  190. distance = cs.match(target, template)
  191. track = cs.get_tracks((distance + 7) * zoom, random.randint(2, 4), cs.ease_out_quart)
  192. #track = cs.get_tracks1(distance)
  193. #track = cs.get_tracks((distance + 7) * zoom, random.randint(1, 2), cs.ease_out_quart)
  194. cs.move_to_gap(track)
  195. #cs.move_to_gap1(distance)
  196. #cs.move_to_gap2(distance)
  197. time.sleep(2)
  198. #cs.browser.close()

同名公众号,更多工具解决方案

image

发表评论

表情:
评论列表 (有 0 条评论,51人围观)

还没有评论,来说两句吧...

相关阅读

    相关 java爬虫破解验证

    > 使用技术:java+Selenium > > 废话: > > 有爬虫,自然就有反爬虫,就像病毒和杀毒软件一样,有攻就有防,两者彼此推进发展。而目前最流行的反爬技术验证码

    相关 Python Java 识别-通杀

    遇到滑块问题 在写爬虫的时候,经常会遇到滑块问题,很多次都想过尝试如何攻破滑块,但是每次都没成功,除了最开始的极验滑块,当时通过原图和滑块图的对比,能够得出缺口坐标,但是

    相关 python模拟滑动验证

    滑动滑块的两个关键点为:(1)生成滑动轨迹(2)控制滑动按钮进行滑动 (1)根据要滑动的距离生成滑动轨迹,此处是模拟人为滑动:先加速滑动滑块,再减速滑动滑块。代码如下: 其