selenium验证码识别方案二:第三方AI库识别验证码之复杂验证码

在这里插入图片描述
这个需要自己注册一个账号:byzhang/root
进入这个接口:
https://www.showapi.com/apiGateway/view?apiCode=184
在这里插入图片描述
点击详情然后选择:
在这里插入图片描述
选择下载SDK
在这里插入图片描述
解压缩后放到lib目录下
在这里插入图片描述
详细代码如下:

import requests
from urllib import parse
#全局请求头
files = {}
headers = {}
body = {}
timeouts = {}
resHeader = {}

class ShowapiRequest:
    def __init__(self, url, my_appId, my_appSecret):
        self.url = url
        self.my_appId = my_appId
        self.my_appSecret = my_appSecret
        body["showapi_appid"] = my_appId
        body["showapi_sign"] = my_appSecret
        headers["User-Agent"] = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2427.7 Safari/537.36"

    def addFilePara(self, key, value_url):
        files[key] = open(r"%s" % (value_url), 'rb')
        return self

    def addHeadPara(self, key, value):
        headers[key] = value
        return self

    def addBodyPara(self, key, value):
        body[key] = value
        return self
    #设置连接时间和读取时间
    def setTimeout(self, connecttimout, readtimeout):
        timeouts["connecttimout"] = connecttimout
        timeouts["readtimeout"] = readtimeout
        return self


    def get(self):
        get_url = self.url + "?" + parse.urlencode(body)
        if not timeouts:
            res = requests.get(get_url, headers=headers)
        else:
            timeout = (timeouts["connecttimout"], timeouts["readtimeout"])
            res = requests.get(get_url, headers=headers, timeout=timeouts)
        return res

    def post(self):
        if not timeouts:
            res = requests.post(self.url, files=files, data=body, headers=headers)
        else:
            timeout = (timeouts["connecttimout"], timeouts["readtimeout"])
            res = requests.post(self.url, files=files, data=body, headers=headers, timeout=timeout)
        return res

实例:
需要先下载个包
pip install requests
原始实例:

# python3.6.5
# 需要引入requests包 :运行终端->进入python/Scripts ->输入:pip install requests
from lib.ShowapiRequest import ShowapiRequest

r = ShowapiRequest("http://route.showapi.com/184-4","my_appId","my_appSecret" )
r.addFilePara("image", "替换为你的文件")
r.addBodyPara("typeId", "34")
r.addBodyPara("convert_to_jpg", "0")
r.addBodyPara("needMorePrecise", "0")
res = r.post()
print(res.text) # 返回信息

“my_appId”,"my_appSecret"需要找到密钥
进入万维网我的应用里
在这里插入图片描述
490656
da211c7555a647b4bc32c0ce71cf5f11

好 接下来是开始完善代码验证验证码

# python3.6.5
# 需要引入requests包 :运行终端->进入python/Scripts ->输入:pip install requests
from seelenium_project.lib.ShowapiRequest import ShowapiRequest

#通过后台AI算法来识别验证码
r = ShowapiRequest("http://route.showapi.com/184-4","490656","da211c7555a647b4bc32c0ce71cf5f11" ) #密钥和密码
r.addFilePara("image","test.jpg") #图片名称
r.addBodyPara("typeId", "34")
r.addBodyPara("convert_to_jpg", "0")
r.addBodyPara("needMorePrecise", "0")
res = r.post()
print(res.text) # 返回信息

#取showapi_res_body中的result
body=res.json()['showapi_res_body']
print(body)

#取出验证码
print(body['Result'])  #yf66j这个就是提取出来的验证码

运用到项目中见后面详情!!!

获取图片到验证

def get_code(driver):
    # 获取验证码图片
    t = time.time()
    path = os.path.dirname(os.path.dirname(__file__)) + '\\screenshopts'
    picture_name1 = path + '\\' + str(t) + '.png'

    driver.save_screenshot(picture_name1)

    ce = driver.find_element_by_xpath('/html/body/div[1]/div/form/div[6]/img')

    left = ce.location['x']
    top = ce.location['y']
    right = ce.size['width'] + left
    height = ce.size['height'] + top

    dpr = driver.execute_script('return window.devicePixelRatio')

    print(dpr)
    im = Image.open(picture_name1)
    img = im.crop((left*dpr, top*dpr, right*dpr, height*dpr))

    t = time.time()

    picture_name2 = path + '\\' + str(t) + '.png'
    img.save(picture_name2)  # 这里就是截取到的验证码图片

    #这里是复杂验证码获取

    r = ShowapiRequest("http://route.showapi.com/184-4", "290728", "1bd001f23c874581aac4db788a92c71d")

    r.addFilePara("image", picture_name2)
    r.addBodyPara("typeId", "34")
    r.addBodyPara("convert_to_jpg", "0")
    r.addBodyPara("needMorePrecise", "0")
    res = r.post()
    print(res.text)  # 返回信息
    # 取showapi_res_body中的result
    body = res.json()['showapi_res_body']
    print(body)

    # 取出验证码
    print(body['Result'])

main函数里

 #获取验证码
    driver=webdriver.Chrome()
    driver.get('http://localhost:8080/jpress/user/register')
    driver.maximize_window()
    util.get_code(driver)
    print(util.get_code(driver))

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值