前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python滑动验证码_python编程是啥

python滑动验证码_python编程是啥

作者头像
全栈程序员站长
发布2022-09-27 14:24:36
4120
发布2022-09-27 14:24:36
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君。

程序功能:程序模仿登入京东主页,自动输入帐号和密码,完成滑块验证,最后领取每日签京豆

关键难点:80%的难点在于滑块验证

代码语言:javascript
复制
import time
import cv2
import random
import openpyxl
from urllib import request
from selenium import webdriver
import numpy as np
from selenium.webdriver.common.action_chains import ActionChains

#chromdriver下载地址http://npm.taobao.org/mirrors/chromedriver/
#下载好了需要设置好PATH
browser=webdriver.Chrome(executable_path="chromedriver.exe")

def login_myhome():
    handle1=browser.current_window_handle
    _xpath=r"//a[contains(@href,'home')]"
    browser.find_element_by_xpath(_xpath).click()
    time.sleep(2)

    handles=browser.window_handles
    print(handles)
    for newhandle in handles:
        if newhandle != handle1:
            browser.switch_to.window(newhandle)
    print(browser.current_window_handle)

    #TODO:login my jingbean home
    _xpath=r"//a[contains(@href,'myJingBean')]"
    browser.find_element_by_xpath(_xpath).click()
    time.sleep(2)
    handle2=browser.current_window_handle
    #TODO:login jd vip home
    _xpath=r"//li[@class='li-item2']/div/a[contains(@class,'c-btn')]"
    browser.find_element_by_xpath(_xpath).click()
    time.sleep(2)
    #TODO:switch handles
    handles=browser.window_handles
    print(handles)
    for newhandle in handles:
        if newhandle != handle1 and newhandle!=handle2:
            browser.switch_to.window(newhandle)
    print(browser.current_window_handle)
    #TODO: get bean
    _xpath=r"//span[@class='icon-sign']"
    browser.find_element_by_xpath(_xpath).click()
    time.sleep(2)

    browser.quit()


def move(btn,x,y):
    distance=y
    has_gone_dist=0
    remaining_dist=y
    print('btn:')
    print(btn)
    #获取滑块
    element=browser.find_element_by_xpath(btn)
    ActionChains(browser).click_and_hold(on_element=element).perform()
    time.sleep(0.5)

    while remaining_dist>0:
        ratio=remaining_dist / distance
        if ratio<0.1:
            #结束阶段移动较慢
            span=random.randint(3,5)
        elif ratio>0.9:
            #开始阶段移动较慢
            span = random.randint(5,8)
        else:
            #中间部分移动较快
            span = random.randint(15, 20)
        #由于京东验证机制比较严格,模仿手动移动,每次移动上下有5像素的偏差
        ActionChains(browser).move_by_offset(span,random.randint(-5,5)).perform()
        remaining_dist-=span
        has_gone_dist+=span
        time.sleep(random.randint(5,20)/100)

    print(remaining_dist)
    #回移一下
    ActionChains(browser).move_by_offset(remaining_dist,random.randint(-5,5)).perform()
    #松下按键
    ActionChains(browser).release(on_element=element).perform()
    time.sleep(2)

def getPic():
    #取登录图片的大图
    img1=r'//div/div[@class="JDJRV-bigimg"]/img'
    #取登录图片的小图
    img2=r'//div/div[@class="JDJRV-smallimg"]/img'
    bigimg=browser.find_element_by_xpath(img1).get_attribute("src")
    smallimg = browser.find_element_by_xpath(img2).get_attribute("src")

    #下载两个图片到本地,是两张彩色的原图
    request.urlretrieve(bigimg,'backimg')
    request.urlretrieve(smallimg,'slideimg')
    #获取图片并做灰化
    template=cv2.imread('backimg',0)
    block=cv2.imread('slideimg',0)
    #将灰化的图片进行保存
    blockName='block.jpg'
    templateName='template.jpg'
    cv2.imwrite(blockName,block)
    cv2.imwrite(templateName, template)
    #对小滑块又进行了一次灰化处理
    block=cv2.imread(blockName)
    block=cv2.cvtColor(block,cv2.COLOR_BGR2GRAY)
    block=abs(255-block)
    cv2.imwrite(blockName,block)
    #将进终灰化过的图片读出来
    block=cv2.imread(blockName)
    template=cv2.imread(templateName)

    #获取偏移量
    #result返回block在template的位置,返回一个数组
    result=cv2.matchTemplate(block,template,cv2.TM_CCOEFF_NORMED)
    #print(result)
    x,y=np.unravel_index(result.argmax(),result.shape)

    print("x is {} y is {}".format(x,y))

    btn='//div[@class="JDJRV-slide-inner JDJRV-slide-btn"]'
    move(btn,x,int(y / 1.3))

def getpersoninfo(channel):
    #读取本地存放url,帐号,密码和文件。
    wb=openpyxl.load_workbook('personinfo.xlsx')
    sheet=wb[channel]
    url=sheet['A2'].value
    username=sheet['B2'].value
    pwd=sheet['C2'].value
    return url,username,pwd

def login():
    channel='jd'
    info=getpersoninfo(channel)
    url=info[0]
    username=info[1]
    password=info[2]
    browser.get(url)
    browser.maximize_window()
    link_loin=browser.find_element_by_link_text('你好,请登录')
    link_loin.click()
    time.sleep(1)
    account_login=browser.find_element_by_link_text('账户登录')
    account_login.click()
    time.sleep(1)
    user=browser.find_element_by_id('loginname')
    user.clear()
    pwd=browser.find_element_by_id('nloginpwd')
    pwd.clear()
    user.send_keys(username)
    pwd.send_keys(password)
    time.sleep(1)
    submit=browser.find_element_by_id('loginsubmit')
    submit.click()
    time.sleep(2)

def main():
    #TODO: login url
    login()
    #TODO: getPic and slider identify
    while True:
        try:
            getPic()
            #每次滑块验证等待5秒
            time.sleep(5)
        except:
            print("登入成功")
            break
    time.sleep(2)

    login_myhome()


if __name__=='__main__':
    main()

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/192401.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档