模拟登录超星

模拟登录超星

因为超星的验证码比较简单,直接用tesserocr多次识别。需要多久识别出来全靠脸。不知道还有没有其它方法,望大佬指教。

from selenium import webdriver
from PIL import Image
import tesserocr
import re
import os #删除截图和验证码


def trylog(p_address,img_address,password):
    try:#多次尝试破解,次数足够多就能成功
        for i in range(50):
            get_image(p_address,img_address)
            code = get_text(img_address)
            if code.isdigit():
                break
            else:
                numL = re.findall(r'\d',code)#寻找数字
                cL =''
                if len(numL)==4:#超星验证码都是4个数字
                    for j in range(len(numL)):
                        cL +=str(numL[j])
                    code = cL
                    break
            button_newcode = browser.find_element_by_xpath('//*[@id="numVerCode_tr"]/td[3]/a')
            button_newcode.click()
            print("\r尝试中请稍后"+"-"*(i%10)+">",end=' ')
        else:
            i = i+1
            get_image(p_address,img_address)
            code = get_text(img_address)
            code = get_text(img_address)
        input_password = browser.find_element_by_xpath('//*[@id="passwordId"]')#找到输入密码的标签
        input_password.clear()#清空密码项
        input_password.send_keys(password)#输入密码
        input_code = browser.find_element_by_xpath('//*[@id="numcode"]')
        input_code.clear()
        input_code.send_keys(code)#输入验证码
        button = browser.find_element_by_xpath('//*[@id="form"]/table/tbody/tr[7]/td[2]/label/input')
        button.click()
    except:
        pass
def get_image(p_address,img_address):
    """获取取验证码图片"""
    try:
        browser.save_screenshot(p_address)#浏览器截图
        img = browser.find_element_by_xpath('//*[@id="numVerCode"]')#获取验证码位置
        location = img.location#定位验证码
        size = img.size
        #验证码位置偏移矫正
        rangle = (int(location['x']+150),int(location['y']+80),int(location['x']+size['width']+180),int(location['y']+size['height']+95))
        imge = Image.open(p_address)
        frame = imge.crop(rangle)
        frame = frame.convert('RGB')
        frame.save(img_address)#保存图片
    except:
        print("保存验证码失败")    
def get_text(img_address):
    """读取验证码"""
    try:
        image = Image.open(img_address)
        img_gray = image.convert("L")
        result = tesserocr.image_to_text(img_gray,lang='eng')#使用英文语言包识别效率高一点
        return result
    except:
        return ""

import time
url = "http://i.mooc.chaoxing.com/space/"
log_url = "http://passport2.chaoxing.com/login?fid=&refer=http://i.mooc.chaoxing.com"#登录页面
account = ""#账号
password = ""#密码
p_address = "jietu.png"#截图地址
img_address = "save.jpg"
try:
    browser = webdriver.Chrome()
    time.sleep(3)
    browser.maximize_window()#浏览器最大化
    #打开网页
    browser.get(url)
    #写入账号密码
    input_account = browser.find_element_by_id("unameId")#找到输入账号的标签
    input_account.clear()
    input_account.send_keys(account)
    #填验证码
    for time in range(10):#ocr训练后可以提高登录速度
        print(f"第{time}轮尝试")
        trylog(p_address,img_address,password)
        if url in browser.current_url:
            print('\n登录成功!')
            os.system("del/f/s/q "+p_address)
            os.system("del/f/s/q "+img_address)
            break
    else:
        raise"登录失败,请再试一次"
except Exception as e:
    print('错误类型是',e.__class__.__name__)
    print('错误明细是',e)
    browser.close()

你可能感兴趣的:(模拟登录超星)