python滑动验证码_python编程是啥

python滑动验证码_python编程是啥程序功能:程序模仿登入京东主页,自动输入帐号和密码,完成滑块验证,最后领取每日签京豆关键难点:80%的难点在于滑块验证importtimeimportcv2importrandomimportopenpyxlfromurllibimportrequestfromseleniumimportwebdriverimportnumpyasnpfromselenium.webdriver.common.action_chainsimportActionChai

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

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

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

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()

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

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

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • 详解BP神经网络

    BackPropagationNeuronNetWok  BP神经网络学习算法可以说是目前最成功的神经网络学习算法。显示任务中使用神经网络时,大多数是使用BP算法进行训练.  在我看来BP神经网络就是一个”万能的模型+误差修正函数“,每次根据训练得到的结果与预想结果进行误差分析,进而修改权值和阈值,一步一步得到能输出和预想结果一致的模型。举一个例子:比如某厂商生产一种产品,投放到市场之…

  • mysql和oracle的sql区别有什么_orical与mysql

    mysql和oracle的sql区别有什么_orical与mysqlMysql与Oracle区别1.Oracle是大型数据库而Mysql是中小型数据库,Oracle市场占有率达40%,Mysql只有20%左右,同时Mysql是开源的而Oracle价格非常高。2.Oracle支持大并发,大访问量,是OLTP最好的工具。3.安装所用的空间差别也是很大的,Mysql安装完后才152M而Oracle有3G左右,且使用的时候Oracle占用特别大的内存空间和其他机器性…

  • 数据结构中ElemType是什么意思

    ElemType是数据结构的书上为了说明问题而用的一个词。它是elementtype(“元素的类型”)的简化体。 因为数据结构是讨论抽象的数据结构和算法的,一种结构中元素的类型不一定是整型、字符型、浮点型或者用户自定义类型,为了不重复说明,使用过程中用“elemtype”代表所有可能的数据类型,简单明了的概括了整体。在算法中,除特别说明外,规定ElemType的默认是int型。elem是单词…

  • android代码设置点击涟漪,android – 为自定义CompoundButton添加涟漪效果

    android代码设置点击涟漪,android – 为自定义CompoundButton添加涟漪效果我有以下自定义CompoundButton:publicclassCustomCompoundButtonextendsCompoundButton{publicCustomCompoundButton(Contextcontext){this(context,null);}publicCustomCompoundButton(Contextcontext,Attribut…

  • Flask 让jsonify返回的json串支持中文显示

    Flask 让jsonify返回的json串支持中文显示用flask时遇到了返回字符串支持中文显示的问题,在web端显示的是utf-8的编码,而不是中文,如下图。虽然不影响接口的读取,但是可读性太差,于是研究了一下怎么直接显示成中文。最后找到了解决方案如下,在配置中加入下面一行代码就OK了。app.config[‘JSON_AS_ASCII’]=Falsejson.dumps()解决同样的问题可以加入ensure_ascii=False参考资料:

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号