python实现滑动京东滑块验证码

前置的一些配置

from urllib import request
import cv2
from selenium import webdriver
# from random import random
import pyautogui
from numpy import random


class 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.offset
        pyautogui.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 random


class 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.offset
        pyautogui.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

如有侵权,望告删

  • 3
    点赞
  • 51
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
### 回答1: 可以使用Python的PIL库来实现滑动图片验证码,具体步骤如下: 1. 读取图片,使用PIL库中的Image.open()方法; 2. 计算两张图片的像素差,使用PIL库中的ImageChops.difference()方法; 3. 计算滑块的位置,使用PIL库中的Image.histogram()方法; 4. 移动滑块,使用Selenium库中的ActionChains.drag_and_drop()方法; 5. 提交验证,使用Selenium库中的WebDriver.submit()方法。 ### 回答2: Python可以使用第三方库如Pillow和OpenCV实现滑动图片验证码。 首先,我们需要生成一个带有滑块验证码图片。可以使用Python的Pillow库来生成验证码图片。我们可以使用Pillow的Image和ImageDraw模块来创建一个空白的图片,并在图片上添加文字和干扰线,然后将其中一部分作为滑块。 ```python from PIL import Image, ImageDraw # 创建一个空白图片 image = Image.new('RGB', (width, height), (255, 255, 255)) draw = ImageDraw.Draw(image) # 在图片上添加文字和干扰线 # 添加滑块 # 保存验证码图片 image.save('captcha.png') ``` 接下来,我们需要实现验证滑块位置的功能。可以使用OpenCV库来处理验证码图片和滑块图片之间的关系。我们可以使用OpenCV的模板匹配算法来找出滑块验证码图片中的位置。首先,我们需要将验证码图片和滑块图片加载进来,并将它们转换成灰度图像。然后,我们可以使用OpenCV库中的matchTemplate方法来查找滑块验证码图片中的位置。 ```python import cv2 # 加载验证码图片和滑块图片 captcha_image = cv2.imread('captcha.png') slider_image = cv2.imread('slider.png') # 将图片转换成灰度图像 captcha_gray = cv2.cvtColor(captcha_image, cv2.COLOR_BGR2GRAY) slider_gray = cv2.cvtColor(slider_image, cv2.COLOR_BGR2GRAY) # 使用模板匹配算法查找滑块验证码图片中的位置 result = cv2.matchTemplate(captcha_gray, slider_gray, cv2.TM_CCOEFF_NORMED) # 获取滑块验证码图片中的位置 min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result) ``` 最后,我们可以将找到的滑块位置作为结果返回。 ```python slider_position = max_loc[0] # 滑块验证码图片中的横坐标位置 # 返回滑块位置 return slider_position ``` 以上是使用Python实现滑动图片验证码的简单示例。具体实现可以根据实际需求进行调整和优化。 ### 回答3: Python可以通过使用第三方库来实现滑动图片验证码。 首先,我们可以使用Pillow库(一个Python图像处理库)来加载验证码图片。可以使用`Image.open()`方法打开验证码图片。 接下来,要实现滑动验证码的功能,我们需要通过一些算法来计算滑动的距离。可以使用OpenCV库(一个图像处理和计算机视觉库)来处理图片并获取滑块的位置。可以使用`cv2.matchTemplate()`方法来寻找滑块的位置。 然后,我们需要通过模拟滑动动作来完成验证操作。可以使用Selenium库(一个用于Web应用程序的自动化测试库)来模拟用户在网页上的操作。可以使用`ActionChains`类和`draggable()`方法来模拟滑动滑块的操作。 最后,我们还可以使用Flask库(一个Python web框架)来搭建一个简单的网页应用来展示滑动图片验证码,并实现滑动验证码的功能。 综上所述,通过使用Pillow库加载验证码图片,使用OpenCV库获取滑块位置,使用Selenium库模拟滑动操作,并使用Flask库搭建网页应用,我们可以实现Python滑动图片验证码功能。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值