Python Selenium + phantomJS 模拟登陆教务管理系统 “抢课”
# _*_coding:utf-8_*_
from selenium import webdriver from selenium.webdriver.common.action_chains import ActionChains import os,urllib.request,sys,importlib,time,winreg
def Login_in(username,password): #教务管理系统网址 PostUrl = "" #验证码网址 SecretCodeUrl = "" #通过win注册表获得桌面路径地址 key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, \ r'Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders', ) cwd= winreg.QueryValueEx(key, "Desktop")[0] address = str(cwd) + "/image.jpg" driver.get(PostUrl) driver.find_element_by_name("txtUserName").send_keys(username) #双input关联 driver.find_element_by_name('Textbox1').click() driver.find_element_by_id('TextBox2').send_keys(password) #验证码 #读取验证码网址、打开本地路径、写入、输入验证码 downpicture = urllib.request.urlopen(SecretCodeUrl).read() local = open(address, 'wb') local.write(downpicture) local.close() print(u"请从桌面寻找你的验证码") print (u"请输入验证码:") SecretCode = input() driver.find_element_by_id('txtSecretCode').send_keys(SecretCode) driver.find_element_by_xpath("//*[@id='Button1']").click() def To_Grab_class(corm,username): directUrl = "http://127.0.0.1/(mil3m1jzac4e0a55aqz5ky45)/xs_main.aspx?xh="+ username driver.get(directUrl) #鼠标悬停 chain = ActionChains(driver) implement = driver.find_element_by_link_text(u"网上选课") chain.move_to_element(implement).perform() if corm == 1 : driver.find_element_by_link_text("校选修课").click() else : driver.find_element_by_link_text("院系选修课").click() def check_all(): checkboxs = driver.find_elements_by_xpath("//input[@type='checkbox']") i=1 for click in checkboxs : i=i+1 if i % 2 == 0 : click.click() driver.find_element_by_id("Button1").click() if __name__ == "__main__": importlib.reload(sys) print(u"请输入学号:") username =input() print(u"请输入密码:") password = input() print(u"校选修课请按1,院系选修课请按2 :") corm = int(input()) #使用的 phentomjs浏览器 此地需要自行更改 driver = webdriver.PhantomJS(executable_path=r"d:\Grab_class\phantomjswindows\bin\phantomjs.exe") Login_in(username,password) To_Grab_class(corm,username) check_all()