首先介绍需要的环境
在cmd中
pip install selenium,紧接着安装Chromedriver或者Firefoxdriver,再添加环境变量。这里主要使用Webdriver库
直接切入主题,上代码
from selenium import webdriver
from aip import AipOcr
from PIL import Image
import time
import os
import re
url= r'http://xk.csust.edu.cn/'
p_path1=r"D:\test1.png"
p_path2=r"D:\test2.png"
def DownValidataCode(p_path1,p_path2): #获取验证码图片
time.sleep(3)
drive.get_screenshot_as_file(p_path1) #网页截图
img = Image.open(p_path1) #利用Image库将验证码截取
box = (307, 339, 428, 391) #通过Photoshop可以看到验证码的像素区域范围
im = img.crop(box) #截取
im.save(p_path2) #保存到本地
def GetValidateCode(p_path): #百度识别验证码,根据百度开发文档写
APP_ID = 'xxxxx'
API_KEY = 'xxxxxxx'
SECRET_KEY = 'xxxxxx'
client = AipOcr(APP_ID, API_KEY, SECRET_KEY)
f= open(p_path,'rb')
img= f.read()
f.close()
a= client.webImage(img) #返回的是一个json
word= re.search(r'\'words\': \'(.*?)\'',str(a))
code=word.group(0).split('\'')
return code[-2] #找到验证码并返回
def PicDelete(p_path1,p_path2): #删除保存在本地的验证码
os.remove(p_path1)
os.remove(p_path2)
print("请确保学号和密码输入正确!!!")
std_num= input("请输入学号:")
password= input("请输入密码:")
drive= webdriver.Chrome()
drive.maximize_window()
drive.get(url)
time.sleep(3)
drive.find_element_by_xpath("//*[@id='m14']").click()
drive.switch_to.frame('frmHomeShow')
drive.find_element_by_xpath("//*[@id='txt_asmcdefsddsd']").send_keys(str(std_num))
succes= 0
while(succes==0): #循环,因为百度识别率太低,经常验证码错误,循环到直至成功为止
drive.find_element_by_id("txt_pewerwedsdfsdff").send_keys(str(password))
drive.find_element_by_id("txt_sdertfgsadscxcadsads").click()
DownValidataCode(p_path1,p_path2)
a= GetValidateCode(p_path2)
print(a)
drive.find_element_by_id("txt_sdertfgsadscxcadsads").send_keys(a)
drive.find_element_by_xpath("//*[@id='divThePanel']/tbody/tr/td/table/tbody/tr[5]/td[2]/input[1]").click()
time.sleep(5)
if drive.current_url != "http://xk.csust.edu.cn/":
#不知道怎么捕捉验证码错误异常,通过网页地址判断是否登陆成功
succes = 1
time.sleep(3)
PicDelete(p_path1,p_path2)
os.system("pause")
#drive.close()
最后用xpath工具提取成绩,即可
版权声明:如无特殊说明,文章均为本站原创,转载请注明出处
本文链接:https://blog.csdn.net/wsad861512140