[爬虫] 京东滑动验证码破解(selenium+python)

写在前面。

非原创,参考的是这篇文章,侵权转自己可见。

 

https://blog.csdn.net/weixin_45943597/article/details/109210659?utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-3.withoutpai&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-3.withoutpai

 

我只是在他的代码上做了稍微的修改,滑动的时候随缘划过,但亲测可过。

 

from urllib import request
from selenium import webdriver
import cv2
import random
import time
import pyautogui


driver = webdriver.Chrome()
driver.maximize_window()
driver.get("https://passport.jd.com/new/login.aspx")
driver.find_element_by_xpath('/html/body/div[2]/div[2]/div[1]/div/div[3]/a').click()
driver.find_element_by_xpath('//*[@id="loginname"]').send_keys('野蛮水蜜桃')
driver.find_element_by_xpath('//*[@id="nloginpwd"]').send_keys('*********')
driver.find_element_by_xpath('//*[@id="loginsubmit"]').click()

while True:

        # 从网页上获取组件
        target = driver.find_element_by_xpath('/html/body/div[4]/div/div/div/div[1]/div[2]/div[1]/img')
        template = driver.find_element_by_xpath('/html/body/div[4]/div/div/div/div[1]/div[2]/div[2]/img')
        # 获取模块的url路径
        src1 = target.get_attribute("src")
        src2 = template.get_attribute("src")
        # 下载图片
        request.urlretrieve(src1,"findPicimg1.jpg")
        request.urlretrieve(src2,"img2.png")


        pic1 = "findPicimg1.jpg"
        pic2 = "img2.png"

        # 读取图片
        target_rgb = cv2.imread(pic1)
        # 图片灰度化
        target_gray = cv2.cvtColor(target_rgb, cv2.COLOR_BGR2GRAY)
        # 读取模块图片
        template_rgb = cv2.imread(pic2, 0)
        # 匹配模块位置
        res = cv2.matchTemplate(target_gray, template_rgb, cv2.TM_CCOEFF_NORMED)
        # 获取最佳匹配位置
        value = cv2.minMaxLoc(res)
        # 返回最佳X坐标
        x = value[2][0]

        print(x)
        w1 = cv2.imread('findPicimg1.jpg').shape[1]
        w2 = target.size['width']
        print(w1,w2)
        x = x / w1 * w2
        # # 按钮坐标
        offset_x,offset_y = 1276,588
        # pyautogui库操作鼠标指针
        pyautogui.moveTo(offset_x, offset_y,
                         duration=0.1 + random.uniform(0, 0.1 + random.randint(1, 100) / 100))
        pyautogui.mouseDown()
        offset_y += random.randint(9, 19)
        pyautogui.moveTo(offset_x + int(x * random.randint(15, 25) / 20), offset_y, duration=0.28)
        offset_y += random.randint(-9, 0)
        pyautogui.moveTo(offset_x + int(x * random.randint(17, 23) / 20), offset_y,
                         duration=random.randint(20, 31) / 100)
        offset_y += random.randint(0, 8)
        pyautogui.moveTo(offset_x + int(x * random.randint(19, 21) / 20), offset_y,
                         duration=random.randint(20, 40) / 100)
        offset_y += random.randint(-3, 3)
        pyautogui.moveTo(offset_x + int(x * random.randint(15, 20) / 20), offset_y,
                         duration=random.randint(20, 40) / 100)
        offset_y += random.randint(-3, 3)
        pyautogui.moveTo(x + offset_x + random.randint(-3, 3), offset_y,
                         duration=0.5 + random.randint(-10, 10) / 100)
        offset_y += random.randint(-2, 2)
        pyautogui.moveTo(x + offset_x + random.randint(-2, 2), offset_y,
                         duration=0.5 + random.randint(-3, 3) / 100)
        pyautogui.moveTo(x + offset_x + random.randint(12, 17), offset_y,
                         duration=0.5 + random.randint(-3, 3) / 100)
        pyautogui.mouseUp()
        time.sleep(1)

 

题外话:为什么还有展示封面这种东西

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值