目录

  • 前置的一些配置
  • 验证码图片的下载
  • 计算偏移
  • 实现滑动效果
  • 完整代码
  • 结尾

前置的一些配置

from urllib import request
import cv2
from selenium import webdriver
# from random import random
import pyautogui
from numpy import randomclass JD_Verification_code():def __init__(self):self.url = 'https://passport.jd.com/new/login.aspx?ReturnUrl=https%3A%2F%2Fwww.jd.com%2F'#京东登录的url地址self.driver = webdriver.Chrome()#实例化一个Chrome浏览器(我个人是把chromedriver.exe放在了Scripts目录下所有没写路径)self.wath = self.driver.implicitly_wait(10)#设置隐式等待时间self.username = '123456789'self.password = '123456789'

验证码图片的下载


因为京东验证码底图和缺口图片都是base64的,所以在这我就用了urllib.request.urlretrieve直接去处理保存到本地了

    def get_image(self):self.driver.get(self.url)self.driver.maximize_window()self.driver.find_element_by_xpath('//*[@id="content"]/div[2]/div[1]/div/div[3]/a').click()self.driver.find_element_by_xpath('//*[@id="loginname"]').send_keys(self.username)self.driver.find_element_by_xpath('//*[@id="nloginpwd"]').send_keys(self.password)self.driver.find_element_by_xpath('//*[@id="loginsubmit"]').click()self.target = self.driver.find_element_by_xpath('//*[@id="JDJRV-wrap-loginsubmit"]/div/div/div/div[1]/div[2]/div[1]/img')self.src = self.target.get_attribute('src')self.template = self.driver.find_element_by_xpath('//*[@id="JDJRV-wrap-loginsubmit"]/div/div/div/div[1]/div[2]/div[2]/img').get_attribute('src')request.urlretrieve(self.src, "target.png")request.urlretrieve(self.template, "temlate.png")

计算偏移


第一个函数先把图片进行灰度化去计算原图的移动偏移,然后第二个函数利用本地的图片尺寸和网页的尺寸计算出缩放比例,再把原本计算出的偏移进一步计算就可以得出网页上的偏移距离

    def FindPic(self,target='target.png', template='temlate.png'):target_rgb = cv2.imread(target)target_gray = cv2.cvtColor(target_rgb, cv2.COLOR_RGB2GRAY)template_rgb = cv2.imread(template, 0)res = cv2.matchTemplate(target_gray, template_rgb, cv2.TM_CCOEFF_NORMED)value = cv2.minMaxLoc(res)# print(value)return value[2][0]def size(self):x = self.FindPic()img = cv2.imread('target.png')w1 = img.shape[1]w2 = self.target.size['width']self.offset = int(x * w2 / w1 + 25)print(self.offset)# self.driver.quit()

实现滑动效果

这一步确实挺难搞的,因为京东有人工智能识别的原因,用selenium滑动就会因为鼠标光标不动等一系列的原因,就算缺口滑动的正确了会不通过,这里查了很久最后采用了前辈用pyautogui库写的一个算法才解决

    def Drag_the(self):The_slider = self.driver.find_element_by_xpath('//*[@id="JDJRV-wrap-loginsubmit"]/div/div/div/div[2]/div[3]')x = The_slider.location.get('x')y = The_slider.location.get('y')print(The_slider.location, ',kw_x = ', x, ',kw_y = ', y)xx = x + self.offsetpyautogui.moveTo(x+24, y+127, duration=0.1)pyautogui.mouseDown()y += random.randint(9, 19)pyautogui.moveTo(x + int(self.offset * random.randint(15, 23) / 20), duration=0.28)y += random.randint(-9, 0)pyautogui.moveTo(x + int(self.offset * random.randint(17, 21) / 20), duration=(random.randint(20, 31)) / 100)y += random.randint(0, 8)pyautogui.moveTo(xx, duration=0.3)pyautogui.mouseUp()

完整代码

from urllib import request
import cv2
from selenium import webdriver
# from random import random
import pyautogui
from numpy import randomclass JD_Verification_code():def __init__(self):self.url = 'https://passport.jd.com/new/login.aspx?ReturnUrl=https%3A%2F%2Fwww.jd.com%2F'self.driver = webdriver.Chrome()self.wath = self.driver.implicitly_wait(10)self.username = '123456789'self.password = '123456789'def get_image(self):self.driver.get(self.url)self.driver.maximize_window()self.driver.find_element_by_xpath('//*[@id="content"]/div[2]/div[1]/div/div[3]/a').click()self.driver.find_element_by_xpath('//*[@id="loginname"]').send_keys(self.username)self.driver.find_element_by_xpath('//*[@id="nloginpwd"]').send_keys(self.password)self.driver.find_element_by_xpath('//*[@id="loginsubmit"]').click()self.target = self.driver.find_element_by_xpath('//*[@id="JDJRV-wrap-loginsubmit"]/div/div/div/div[1]/div[2]/div[1]/img')self.src = self.target.get_attribute('src')self.template = self.driver.find_element_by_xpath('//*[@id="JDJRV-wrap-loginsubmit"]/div/div/div/div[1]/div[2]/div[2]/img').get_attribute('src')request.urlretrieve(self.src, "target.png")request.urlretrieve(self.template, "temlate.png")def FindPic(self,target='target.png', template='temlate.png'):target_rgb = cv2.imread(target)target_gray = cv2.cvtColor(target_rgb, cv2.COLOR_RGB2GRAY)template_rgb = cv2.imread(template, 0)res = cv2.matchTemplate(target_gray, template_rgb, cv2.TM_CCOEFF_NORMED)value = cv2.minMaxLoc(res)# print(value)return value[2][0]def size(self):x = self.FindPic()img = cv2.imread('target.png')w1 = img.shape[1]w2 = self.target.size['width']self.offset = int(x * w2 / w1 + 25)print(self.offset)# self.driver.quit()def Drag_the(self):The_slider = self.driver.find_element_by_xpath('//*[@id="JDJRV-wrap-loginsubmit"]/div/div/div/div[2]/div[3]')x = The_slider.location.get('x')y = The_slider.location.get('y')print(The_slider.location, ',kw_x = ', x, ',kw_y = ', y)xx = x + self.offsetpyautogui.moveTo(x+24, y+127, duration=0.1)pyautogui.mouseDown()y += random.randint(9, 19)pyautogui.moveTo(x + int(self.offset * random.randint(15, 23) / 20), duration=0.28)y += random.randint(-9, 0)pyautogui.moveTo(x + int(self.offset * random.randint(17, 21) / 20), duration=(random.randint(20, 31)) / 100)y += random.randint(0, 8)pyautogui.moveTo(xx, duration=0.3)pyautogui.mouseUp()jd = JD_Verification_code()
jd.get_image()
jd.FindPic()
jd.size()
jd.Drag_the()

结尾

搞了一夜写完的时候天已经亮了就没有写识别错误后的重试循环,加上接触python的时间不长,部分代码写的可能不太行
参考的文章和视频视频链接:
https://blog.csdn.net/qq_36853469/article/details/100580753?ops_request_misc=%25257B%252522request%25255Fid%252522%25253A%252522160707993219724838586592%252522%25252C%252522scm%252522%25253A%25252220140713.130102334…%252522%25257D&request_id=160707993219724838586592&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2alltop_click~default-1-100580753.first_rank_v2_pc_rank_v29&utm_term=%E4%BA%AC%E4%B8%9C%E9%AA%8C%E8%AF%81%E7%A0%81

https://www.bilibili.com/video/BV1cC4y1Y7Wm

如有侵权,望告删

python实现滑动京东滑块验证码相关推荐

  1. Python如何解决“京东滑块验证码”(5)

    前言 本文是该专栏的第51篇,后面会持续分享python爬虫干货知识,记得关注. 多数情况下使用模拟登录会遇到滑块验证码的问题,对于普通的滑块验证码,使用selenium可以轻松解决.但是对于滑块缺失 ...

  2. Python + selenium自动化工具 + 滑块验证码+点选验证码,实现模拟登录“中国铁路网12306”

    文章目录 一.模拟登录"中国铁路网12306 1.引入库 2.初始化 3.将点选验证码图片,通过人工打码,返回目标像素位置(json格式). 4.点选验证码位置得到后,需要鼠标左击进行模拟人 ...

  3. python 实现京东滑块验证码登录

    话不多说直接上代码, 不懂的直接看注释或者私信 import timefrom urllib import requestimport cv2 import pyautogui from seleni ...

  4. python爬虫热点项目—滑块验证码项目(以Bilili为例)

    1.模拟登录的网站: bilibili视频网:https://passport.bilibili.com/login 2. 开发环境 本项目需要用到 io time random selenium P ...

  5. Python破解滑块验证码算法,完美避开人机识别

    | 完美是不可能的,加个震惊!Python破解BiliBili滑块验证码,完美避开人机识别,可以有 准备工作 B站登录页 https://passport.bilibili.com/login pyt ...

  6. Python Selenium破解滑块验证码最新版!

    通过率高达百分之95!真的强! 一.滑块验证码简述 有爬虫,自然就有反爬虫,就像病毒和杀毒软件一样,有攻就有防,两者彼此推进发展.而目前最流行的反爬技术验证码,为了防止爬虫自动注册,批量生成垃圾账号, ...

  7. python滑块验证码

    前言 网上有不少关于滑块验证码的文章,要么就是说了一堆原理断断续续的放一些代码块,要么就是收费!!!都没什么卵用 今天给大家分享一篇Python自动验证滑块验证码的操作!,你只需要复制粘贴就行,原理不 ...

  8. Python高效实现滑块验证码自动操纵

    CDA数据分析师 出品作者:CDALevel Ⅰ 持证人岗位:数据分析师 行业:大数据 众所周知,规范性的网络爬虫可以帮助Decision-maker在低成本下获得想要的信息,不仅如此,做科研.写论文 ...

  9. 滑块验证码通杀方案--python破解代码

    本代码理论上可以通杀市面上所有滑块(如果真出现了识别不到,请自行根据文章末尾的参考内容,自己进行调教) 在线测试地址:simple_ocr 截至2022.7.13,可以破解的有: 1.顶象 2.网易易 ...

最新文章

  1. ArcEngine 直连连接SDE
  2. android自定义美颜相机完整程序,Android OpenGL ES从入门到进阶(一)—— 五分钟开发一款美颜相机...
  3. Python 实现一个全面的单链表
  4. 二叉树的深度优先搜索c语言,C语言 二叉树按层打印、深度优先遍历、二叉树是否对称...
  5. Are We Ready for SDN? Implementation Challenges for Software-Defined Networks
  6. eclipse软件有时会退出弹出一串错误弹框_修复iPhone上的iOS 13软件更新失败错误...
  7. 向net core 3.0进击——April.WebApi从2.2爬到3.0
  8. 架构设计(3)---架构师到底要做什么,他们需要掌握些什么?
  9. (C语言)最长公共子串
  10. 规划以及安装Exchange2007
  11. 根据日志统计出每个用户在站点所呆时间最长的前2个的信息
  12. python:np.vstack, np.hstack
  13. Mac终端打开VsCode
  14. C++ 打印机状态查询之SNMP协议
  15. Kotlin 系列 之 Flow (一)
  16. M个苹果放在N个盘子里,有多少种不同的放法
  17. 数字方法--按零补位
  18. P2455 [SDOI2006]线性方程组
  19. [SECCON CTF 2022] 只两个小题pwn_koncha,rev_babycmp,crypto_pqpq
  20. python实现烤羊肉串(类)

热门文章

  1. AlertDialog,安卓自带取消确认按钮的弹出框
  2. 将图片打造铅笔素描效果
  3. 依赖倒置 php,PHP中的依赖倒置原则
  4. c#自定义控件绘制五角星形状
  5. 简单介绍一下微信直播
  6. C++和C混编时变量和函数的定义和使用
  7. 深度学习初级阶段——全连接神经网络(MLP、FNN)
  8. Softmax函数求导
  9. 计算机房等电位,计算机房屏蔽及等电位防护措施(原稿)
  10. npm 初始化_初始化npm的最佳时间