python3的wxpython界面模擬登錄爬取教務系統實現查詢系統功能2.0版本

前言

今天來補充之前的界面操作系統,爲學生查詢操作系統2.0版本,依舊是用wxpython來做一個可視化的操作界面,用的工具依然是selenium庫,beautifulsoup4庫,還有設計界面的wx,和表格wx.grid,後面兩個只要下載wxpython庫即可

首先是界面操作

裏面的分別對應的文本,按鈕,背景圖片,圖標都有標明

#繼承wx庫裏面的Frame類來使用
class myFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1, 'ZSC-學生查詢系統', pos=(100, 100), size=(1400, 600))
        self.MaxSize = self.Size
        self.MinSize = self.Size
        panel = wx.Panel(self, -1)
        sizer = wx.BoxSizer(wx.HORIZONTAL)
        sizer1 = wx.BoxSizer(wx.VERTICAL)
        sizer3 = wx.BoxSizer(wx.VERTICAL)
        font = wx.Font(15, wx.ROMAN, wx.NORMAL, wx.BOLD)

        #設置窗口以及托盤圖標
        icon = wx.Icon()
        icon.CopyFromBitmap(wx.Bitmap(wx.Image(("E:\py\系統圖片\zsc.jpg"), wx.BITMAP_TYPE_JPEG)))
        self.SetIcon(icon)

        #設置左邊背景學院logo
        image = wx.Image("E:\py\系統圖片\中山學院.png", wx.BITMAP_TYPE_PNG).ConvertToBitmap()
        self.background = wx.StaticBitmap(panel, -1, bitmap=image,style=wx.ALIGN_CENTER)
        sizer1.Add(self.background,proportion=10,flag= wx.ALIGN_CENTER_VERTICAL, border=10)

        #設置靜態‘用戶名'
        self.text1 = wx.StaticText(panel, -1, '用戶名:', style=wx.ALIGN_CENTER)
        self.text1.SetFont(font)
        sizer1.Add(self.text1, proportion=2, flag=wx.LEFT | wx.ALIGN_CENTER_VERTICAL, border=10)

        #用戶名輸入框
        self.text2 = wx.TextCtrl(panel, -1, size=(200, -1), style=wx.TE_LEFT)
        self.text2.SetFont(font)
        sizer1.Add(self.text2, proportion=0, flag=wx.ALIGN_CENTER | wx.EXPAND, border=15)

        #設置靜態'密碼'
        self.text3 = wx.StaticText(panel, -1, '密碼:', style=wx.ALIGN_CENTER)
        self.text3.SetFont(font)
        sizer1.Add(self.text3, proportion=2, flag=wx.LEFT | wx.ALIGN_CENTER_VERTICAL, border=10)

        #密碼輸入框
        self.text4 = wx.TextCtrl(panel, -1, size=(200, -1), style=wx.TE_LEFT|wx.TE_PASSWORD|wx.TE_PROCESS_ENTER)
        self.text4.SetFont(font)
        sizer1.Add(self.text4, proportion=0, flag=wx.ALIGN_CENTER | wx.EXPAND, border=15)

        # 設置獲取成績按鈕
        self.command1 = wx.Button(panel, -1, '→獲取個人成績單')
        self.command1.SetFont(font)
        self.command1.SetBackgroundColour('#3299CC')
        sizer1.Add(self.command1, proportion=5, flag=wx.ALIGN_CENTER | wx.EXPAND, border=10)

        #設置獲取課程表按鈕
        self.command4 = wx.Button(panel, -1, '→獲取個人課程表')
        self.command4.SetFont(font)
        self.command4.SetBackgroundColour('#DBDB70')
        sizer1.Add(self.command4, proportion=5, flag=wx.ALIGN_CENTER | wx.EXPAND, border=10)

        #設置獲取素拓分按鈕
        self.command3 = wx.Button(panel, -1, '→獲取個人素拓分')
        self.command3.SetFont(font)
        self.command3.SetBackgroundColour('#32CC32')
        sizer1.Add(self.command3, proportion=5, flag=wx.ALIGN_CENTER | wx.EXPAND, border=10)

        #設置重置按鈕
        self.command2 = wx.Button(panel, -1, '→重置輸入框')
        self.command2.SetFont(font)
        self.command2.SetBackgroundColour((random.randint(1, 255), random.randint(0, 255), random.randint(0, 255)))
        sizer1.Add(self.command2, proportion=5, flag=wx.ALIGN_CENTER | wx.EXPAND, border=10)

        #設置消息提示文本
        self.text5 = wx.StaticText(panel, -1, '\n\n', style=wx.ALIGN_CENTER)
        self.text5.SetFont(font)
        self.text5.SetForegroundColour('Red')
        sizer1.Add(self.text5, proportion=15, flag=wx.LEFT | wx.ALIGN_CENTER_VERTICAL, border=10)

        #設置個人信息文本
        self.text6 = wx.StaticText(panel, -1, '姓名:')
        self.text7 = wx.StaticText(panel, -1, '學號:')
        self.text8 = wx.StaticText(panel, -1, '學院:')
        sizer1.Add(self.text6, proportion=3, flag=wx.LEFT, border=0)
        sizer1.Add(self.text7, proportion=3, flag=wx.LEFT, border=0)
        sizer1.Add(self.text8, proportion=3, flag=wx.LEFT, border=0)

        #把分佈局全部加入整體頂級佈局
        sizer.Add(sizer1, flag=wx.EXPAND | wx.ALL, border=20)
        sizer.Add(sizer3, flag=wx.EXPAND | wx.ALL, border=20)
        self.grid = mygrid(panel)
        sizer.Add(self.grid, proportion=1, flag=wx.EXPAND | wx.ALL, border=25)

        self.grid.SetSize((200, 400))

        panel.SetSizer(sizer)

        #四個按鈕對應的事件
        self.command1.Bind(wx.EVT_BUTTON, self.command1_event)
        self.command4.Bind(wx.EVT_BUTTON, self.command4_event)
        self.command3.Bind(wx.EVT_BUTTON, self.command3_event)
        self.command2.Bind(wx.EVT_BUTTON, self.command2_event)
        self.Show()

    def command1_event(self, event):
        self.text6.SetLabel('姓名:')
        self.text7.SetLabel('學號:')
        self.text8.SetLabel('學院:')
        self.grid.ff()
        self.grid.Update()
        self.text5.SetLabel(u'\n溫馨提示:\n' + '⚪登錄信息驗證中...')
        self.text5.Update()
        try:
            name = self.text2.GetValue()
            password = self.text4.GetValue()
            courses = get_data_from_zsc1(name,password)
            if courses.result == False:
                self.text5.SetLabel(u'\n溫馨提示:\n' + '×用戶名或密碼錯誤!' )
            else:
                student_course = courses.my_score_list
                self.text5.SetLabel(u'\n溫馨提示:\n' + '√登錄成功並獲得成績!')
                self.text6.SetLabel('姓名:' + courses.my_information[0])
                self.text7.SetLabel('學號:' + courses.my_information[1])
                self.text8.SetLabel('學院:' + courses.my_information[2] )
                try:
                    self.grid.header = grade
                    for i in range(len(self.grid.header)):
                        self.grid.SetColLabelValue(i, self.grid.header[i])
                    self.grid.Update()
                    self.grid.Refresh()
                except:
                    pass
                n1 = len(student_course)
                n2 = len(student_course[0])
                self.grid.InsertRows(0, n1)
                for i in range(0, n1):
                    for j in range(0, n2):
                        self.grid.SetCellValue(i, j, student_course[i][j])
                #self.grid.AutoSize() #自動調整單元格大小
        except:
            self.Message = wx.MessageDialog(self, '賬號或密碼不能爲空!', 'ERROR',wx.ICON_ERROR)
            self.Message.ShowModal()
            self.Message.Destroy()

    def command4_event(self, event):
        self.text6.SetLabel('姓名:')
        self.text7.SetLabel('學號:')
        self.text8.SetLabel('學院:')
        self.grid.ff()
        self.grid.Update()
        self.text5.SetLabel(u'\n溫馨提示:\n' + '⚪登錄信息驗證中...')
        self.text5.Update()
        try:
            name = self.text2.GetValue()
            password = self.text4.GetValue()
            courses = get_data_from_zsc2(name,password)
            if courses.result == False:
                self.text5.SetLabel(u'\n溫馨提示:\n' + '×用戶名或密碼錯誤!' )
            else:
                student_timetable = courses.all_data
                self.text5.SetLabel(u'\n溫馨提示:\n' + '√登錄成功並獲得課程表!')
                self.text6.SetLabel('姓名:' + courses.my_information[0])
                self.text7.SetLabel('學號:' + courses.my_information[1])
                self.text8.SetLabel('學院:' + courses.my_information[2] )
                try:
                    self.grid.header = timetable
                    for i in range(len(self.grid.header)):
                        self.grid.SetColLabelValue(i, self.grid.header[i])
                    self.grid.Update()
                    self.grid.Refresh()
                except:
                    pass
                n1 = len(student_timetable)
                n2 = len(student_timetable[0])
                self.grid.InsertRows(0, n1)
                for i in range(0, n1):
                    for j in range(0, n2):
                        self.grid.SetCellValue(i, j, student_timetable[i][j])
                self.grid.SetCellSize(6, 0, 1, 5) #合併單元格 合併第6行第1個爲1x5的單元格
                #設置行的高度
                for i in range(0, n1):
                    self.grid.SetRowSize(i, 80)
                #設置列的寬度
                for j in range(0, n2):
                    self.grid.SetColSize(j, 120)
                #self.grid.AutoSize()
        except:
            self.Message = wx.MessageDialog(self, '賬號或密碼不能爲空!', 'ERROR',wx.ICON_ERROR)
            self.Message.ShowModal()
            self.Message.Destroy()
    def command3_event(self, event):
        self.Message = wx.MessageDialog(self, '對不起,此功能暫時未開放!', 'ERROR', wx.ICON_ERROR)
        self.Message.ShowModal()
        self.Message.Destroy()
    def command2_event(self, event):
        self.text2.SetValue('')
        self.text4.SetValue('')
        self.text5.SetLabel('\n\n')
        self.text6.SetLabel('姓名:')
        self.text7.SetLabel('學號:')
        self.text8.SetLabel('學院:')
        self.grid.ff()
        self.grid.Update()

效果如下,具體的界面各方面從設置佈局開始,分爲兩個佈局。一個是登錄界面佈局
在這裏插入圖片描述
另一個佈局用在存放gird表格
在這裏插入圖片描述

然後是模擬登錄獲取數據的爬蟲

具體的操作就不說了,之前的博文關於模擬登錄教務系統有細說了,
此爬蟲類主要是爬取學生姓名,學院,學號,以及成績單
(2.0版本增加一個爬取個人課程表的類)

首先是爬取個人成績單的類

class get_data_from_zsc():
    def __init__(self, userAccount, password):
        driver_path = r'E:\py\chromedriver\chromedriver.exe'
        chrome_options = Options()
        chrome_options.add_argument('--headless')
        chrome_options.add_argument('--disable-gpu')

        driver = webdriver.Chrome(executable_path=driver_path, chrome_options=chrome_options)

        driver.get('http://jwgln.zsc.edu.cn/jsxsd/')
        driver.implicitly_wait(1)
        driver.find_element_by_id("userAccount").send_keys(userAccount)
        driver.find_element_by_id("userPassword").send_keys(password)
        driver.find_element_by_xpath('//*[@id="btnSubmit"]').click()  # 用click模擬瀏覽器點擊
        driver.implicitly_wait(1)
        self.my_information = []
        self.my_score_list = []
        self.result = self.if_or_get_data(driver)
    def if_or_get_data(self,driver):
        if '用戶名或密碼錯誤' in driver.page_source:
            driver.close()
            driver.quit()
            return False
        else:
            driver1 = self.get_course1(driver)
            self.get_course2(driver1)
            return True
    def get_course1(self,driver):
        #獲取姓名,學號
        bs = BeautifulSoup(driver.page_source, 'lxml')
        information = list(bs.find('div',attrs ={'class':"block1text"}))
        self.my_information.append(information[0].split(":")[1])
        self.my_information.append(information[2].split(":")[1])
        #獲取成績
        driver.find_element_by_xpath('//*[@class="block7"]').click()
        driver.implicitly_wait(1)
        driver.find_element_by_xpath('//*[@id="btn_query"]').click()
        bs = BeautifulSoup(driver.page_source, 'lxml')
        my_score_detail = bs.find_all(name='td')[1:]
        my_score_detail = list(my_score_detail)
        score_list = [i.string for i in my_score_detail]
        for i in range(0, len(score_list), 14):
            course_list = []
            for j in range(i+1, i + 14):
                if score_list[j]== None:
                    course_list.append('\n')
                else:
                    course_list.append(score_list[j])
            self.my_score_list.append(course_list)
        return  driver
    #獲得學院
    def get_course2(self, driver):
        driver.find_element_by_xpath('//*[@title="培養管理"]').click()
        driver.implicitly_wait(1)
        driver.find_element_by_xpath('//*[@href="/jsxsd/pyfa/pyfa_query"]').click()
        # 用bs4進行數據篩選
        bs = BeautifulSoup(driver.page_source, 'lxml')
        my_score_detail = bs.find_all(name='td', attrs={'align': "left"})
        my_score_detail = list(my_score_detail)
        my_score_list = [i.string for i in my_score_detail]
        Map = dict()
        for i in range(len(my_score_list)):
            if i % 4 == 3:
                if my_score_list[i] not in Map.keys():
                    Map[my_score_list[i]] = 0
                else:
                    Map[my_score_list[i]] = Map[my_score_list[i]] + 1
        college = max(Map.keys(), key=(lambda x: Map[x]))
        self.my_information.append(college)
        driver.close()
        driver.quit()

然後是爬取個人課程表的類

class get_data_from_zsc2():
    def __init__(self, userAccount, password):
        driver_path = r'E:\py\chromedriver\chromedriver.exe'
        chrome_options = Options()
        chrome_options.add_argument('--headless')
        chrome_options.add_argument('--disable-gpu')

        driver = webdriver.Chrome(executable_path=driver_path, chrome_options=chrome_options)
        self.all_data = []
        driver.get('http://jwgln.zsc.edu.cn/jsxsd/')
        driver.implicitly_wait(1)
        driver.find_element_by_id("userAccount").send_keys(userAccount)
        driver.find_element_by_id("userPassword").send_keys(password)
        driver.find_element_by_xpath('//*[@id="btnSubmit"]').click()  # 用click模擬瀏覽器點擊
        driver.implicitly_wait(1)
        self.my_information = []
        self.my_score_list = []
        self.result = self.if_or_get_data(driver)
    def if_or_get_data(self,driver):
        if '用戶名或密碼錯誤' in driver.page_source:
            driver.close()
            driver.quit()
            return False
        else:
            driver1 = self.get_course1(driver)
            self.get_course2(driver1)
            return True
    def get_course1(self,driver):
        #獲取姓名,學號
        bs = BeautifulSoup(driver.page_source, 'lxml')
        information = list(bs.find('div',attrs ={'class':"block1text"}))
        self.my_information.append(information[0].split(":")[1])
        self.my_information.append(information[2].split(":")[1])
        #獲取個人課程表
        driver.find_element_by_xpath('//*[@class="block4"]').click()
        driver.implicitly_wait(1)
        driver.find_element_by_xpath('//*[@href="/jsxsd/xskb/xskb_list.do"]').click()
        time.sleep(1)
        #頁面彈窗點擊確認
        try:
            alt = driver.switch_to_alert()
            alt.accept()
        except:
            pass
        try:
            Select(driver.find_element_by_id("xnxq01id")).select_by_value("2019-2020-2").click()
        except:
            pass
        soup = BeautifulSoup(driver.page_source, 'lxml')
        page = soup.find_all('div', attrs={'class': "kbcontent"})
        teachers1, teachers2 = [], []
        weeks1, weeks2 = [], []
        classrooms1, classrooms2 = [], []
        for i in page:
            teachers1.append(i.find('font', attrs={'title': '老師'}))
            weeks1.append(i.find('font', attrs={'title': '周次(節次)'}))
            classrooms1.append(i.find('font', attrs={'title': '教室'}))
        my_detail = list(page)
        for i in teachers1:
            if i == None:
                teachers2.append('\n')
            else:
                teachers2.append(i.string)
        for i in weeks1:
            if i == None:
                weeks2.append('\n')
            else:
                weeks2.append('\n' + i.string)
        for i in classrooms1:
            if i == None:
                classrooms2.append('\n')
            else:
                classrooms2.append('\n' + i.string)
        pitch_number = ['(上午)\n第1,2節\n(08:00-08:45)\n(08:55-09:40)', '(上午)\n第3,4節\n(10:00-10:45)\n(10:55-11:40)',
                        '(下午)\n第5,6節\n(14:30-15:15)\n(15:25-16:10)', '(下午)\n第7,8節\n(16:20-16:05)\n(17:15-18:00)',
                        '(晚上)\n第9,10節\n(19:30-20:15)\n(20:25-21:10)', '第11,12節', '第13,14節']
        temp = []
        temp.append(pitch_number[0])
        num = 0
        pnum = 0
        for i in range(len(my_detail)):
            if my_detail[i].text == '\xa0':
                temp.append('\n\n\n')
            else:
                temp.append(my_detail[i].text.split(teachers2[i])[0] + '\n' + teachers2[i] + weeks2[i] + classrooms2[i])
            num = num + 1
            if num == 7:
                self.all_data.append(temp)
                temp = []
                pnum = pnum + 1
                temp.append(pitch_number[pnum])
                num = 0
        page2 = soup.find('td', attrs={'colspan': "7"})
        BZ = ['備註:' + page2.text, '\n', '\n', '\n', '\n', '\n', '\n', '\n']
        self.all_data.append(BZ)
        return  driver
    #獲得學院
    def get_course2(self, driver):
        driver.find_element_by_xpath('//*[@title="培養管理"]').click()
        driver.implicitly_wait(1)
        driver.find_element_by_xpath('//*[@href="/jsxsd/pyfa/pyfa_query"]').click()
        # 用bs4進行數據篩選
        bs = BeautifulSoup(driver.page_source, 'lxml')
        my_score_detail = bs.find_all(name='td', attrs={'align': "left"})
        my_score_detail = list(my_score_detail)
        my_score_list = [i.string for i in my_score_detail]
        Map = dict()
        for i in range(len(my_score_list)):
            if i % 4 == 3:
                if my_score_list[i] not in Map.keys():
                    Map[my_score_list[i]] = 0
                else:
                    Map[my_score_list[i]] = Map[my_score_list[i]] + 1
        college = max(Map.keys(), key=(lambda x: Map[x]))
        self.my_information.append(college)
        driver.close()
        driver.quit()

最後是wx.grid的表格類

依舊是繼承wx.grid.Grid,來重構類

class mygrid(wx.grid.Grid):
    def __init__(self, parent):
        wx.grid.Grid.__init__(self, parent=parent, id=-1)
        #self.CreateGrid(0, 7)
        self.SetDefaultCellBackgroundColour('#BFD8D8')#parent.BackgroundColour
        self.SetDefaultCellTextColour("#000000")
        self.CreateGrid(0, 13) #設置初始化時的行數和列數
        #爲列定義初始化名字
        for i in range(13):
            self.SetColLabelValue(i,'列名')
        self.header = []
     #初始化函數,作用跟上面差不多
    def ff(self):
        self.ClearGrid() #清除表格數據
        if self.GetNumberRows()>0:
            self.DeleteRows(0, self.GetNumberRows()) #刪除所有行
        #self.DeleteCols(0, self.GetNumberCols())
        for i in range(13):
            self.SetColLabelValue(i, '列名')
        self.Update()
        self.Refresh()
        self.header = []

當然,要奉上圖片資源

這個是背景logo,即是‘中山學院.png’ 下載後更改儲存圖片的地址即可,圖標資源就不提供了,太大了,就是圓標校徽
在這裏插入圖片描述

整體爬取成功的界面樣子

1.0版本是下面的樣子
在這裏插入圖片描述
2.0版本多了一個爬取個人課程表的功能
成績單
在這裏插入圖片描述
課程表
在這裏插入圖片描述

最後的最後,奉上所有代碼

將chromedriver.exe的存儲位置改好,對應的庫下載好,圖片資源最好下載設置後位置,不弄的話將對應的設置圖片代碼行註釋掉,應該也可以運行,只是會缺少相應的圖片

# -*-coding:utf-8-*-
'''
@author Himit_ZH
Date:2020.02.05
'''
from selenium import webdriver # 從selenium導入webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.select import Select
from bs4 import BeautifulSoup
import wx
import wx.grid
import random
import time


sutuo = ['活動名稱', '獲得學分', '班級審覈狀態', '院系審覈狀態', '學校審覈狀態', '審覈截止日期', '申報成功']
grade = ['開課學期', '課程編號', '課程名稱', '總成績', '學分',
                  '平時成績', '期中成績', '實驗成績', '期末成績', '課程屬性',
                  '課程性質', '備註', '考試性質']
timetable = ['課程時間','星期一','星期二','星期三','星期四','星期五','星期六','星期日',
             '無','無','無','無','無']
class myFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1, 'ZSC-學生查詢系統', pos=(100, 100), size=(1400, 600))
        self.MaxSize = self.Size
        self.MinSize = self.Size
        panel = wx.Panel(self, -1)
        sizer = wx.BoxSizer(wx.HORIZONTAL)
        sizer1 = wx.BoxSizer(wx.VERTICAL)
        sizer3 = wx.BoxSizer(wx.VERTICAL)
        font = wx.Font(15, wx.ROMAN, wx.NORMAL, wx.BOLD)

        #設置窗口以及托盤圖標
        icon = wx.Icon()
        icon.CopyFromBitmap(wx.Bitmap(wx.Image(("E:\py\系統圖片\zsc.jpg"), wx.BITMAP_TYPE_JPEG)))
        self.SetIcon(icon)

        #設置左邊背景學院logo
        image = wx.Image("E:\py\系統圖片\中山學院.png", wx.BITMAP_TYPE_PNG).ConvertToBitmap()
        self.background = wx.StaticBitmap(panel, -1, bitmap=image,style=wx.ALIGN_CENTER)
        sizer1.Add(self.background,proportion=10,flag= wx.ALIGN_CENTER_VERTICAL, border=10)

        #設置靜態‘用戶名'
        self.text1 = wx.StaticText(panel, -1, '用戶名:', style=wx.ALIGN_CENTER)
        self.text1.SetFont(font)
        sizer1.Add(self.text1, proportion=2, flag=wx.LEFT | wx.ALIGN_CENTER_VERTICAL, border=10)

        #用戶名輸入框
        self.text2 = wx.TextCtrl(panel, -1, size=(200, -1), style=wx.TE_LEFT)
        self.text2.SetFont(font)
        sizer1.Add(self.text2, proportion=0, flag=wx.ALIGN_CENTER | wx.EXPAND, border=15)

        #設置靜態'密碼'
        self.text3 = wx.StaticText(panel, -1, '密碼:', style=wx.ALIGN_CENTER)
        self.text3.SetFont(font)
        sizer1.Add(self.text3, proportion=2, flag=wx.LEFT | wx.ALIGN_CENTER_VERTICAL, border=10)

        #密碼輸入框
        self.text4 = wx.TextCtrl(panel, -1, size=(200, -1), style=wx.TE_LEFT|wx.TE_PASSWORD|wx.TE_PROCESS_ENTER)
        self.text4.SetFont(font)
        sizer1.Add(self.text4, proportion=0, flag=wx.ALIGN_CENTER | wx.EXPAND, border=15)

        # 設置獲取成績按鈕
        self.command1 = wx.Button(panel, -1, '→獲取個人成績單')
        self.command1.SetFont(font)
        self.command1.SetBackgroundColour('#3299CC')
        sizer1.Add(self.command1, proportion=5, flag=wx.ALIGN_CENTER | wx.EXPAND, border=10)

        #設置獲取課程表按鈕
        self.command4 = wx.Button(panel, -1, '→獲取個人課程表')
        self.command4.SetFont(font)
        self.command4.SetBackgroundColour('#DBDB70')
        sizer1.Add(self.command4, proportion=5, flag=wx.ALIGN_CENTER | wx.EXPAND, border=10)

        #設置獲取素拓分按鈕
        self.command3 = wx.Button(panel, -1, '→獲取個人素拓分')
        self.command3.SetFont(font)
        self.command3.SetBackgroundColour('#32CC32')
        sizer1.Add(self.command3, proportion=5, flag=wx.ALIGN_CENTER | wx.EXPAND, border=10)

        #設置重置按鈕
        self.command2 = wx.Button(panel, -1, '→重置輸入框')
        self.command2.SetFont(font)
        self.command2.SetBackgroundColour((random.randint(1, 255), random.randint(0, 255), random.randint(0, 255)))
        sizer1.Add(self.command2, proportion=5, flag=wx.ALIGN_CENTER | wx.EXPAND, border=10)

        #設置消息提示文本
        self.text5 = wx.StaticText(panel, -1, '\n\n', style=wx.ALIGN_CENTER)
        self.text5.SetFont(font)
        self.text5.SetForegroundColour('Red')
        sizer1.Add(self.text5, proportion=15, flag=wx.LEFT | wx.ALIGN_CENTER_VERTICAL, border=10)

        #設置個人信息文本
        self.text6 = wx.StaticText(panel, -1, '姓名:')
        self.text7 = wx.StaticText(panel, -1, '學號:')
        self.text8 = wx.StaticText(panel, -1, '學院:')
        sizer1.Add(self.text6, proportion=3, flag=wx.LEFT, border=0)
        sizer1.Add(self.text7, proportion=3, flag=wx.LEFT, border=0)
        sizer1.Add(self.text8, proportion=3, flag=wx.LEFT, border=0)

        #把分佈局全部加入整體頂級佈局
        sizer.Add(sizer1, flag=wx.EXPAND | wx.ALL, border=20)
        sizer.Add(sizer3, flag=wx.EXPAND | wx.ALL, border=20)
        self.grid = mygrid(panel)
        sizer.Add(self.grid, proportion=1, flag=wx.EXPAND | wx.ALL, border=25)

        self.grid.SetSize((200, 400))

        panel.SetSizer(sizer)

        #四個按鈕對應的事件
        self.command1.Bind(wx.EVT_BUTTON, self.command1_event)
        self.command4.Bind(wx.EVT_BUTTON, self.command4_event)
        self.command3.Bind(wx.EVT_BUTTON, self.command3_event)
        self.command2.Bind(wx.EVT_BUTTON, self.command2_event)
        self.Show()

    def command1_event(self, event):
        self.text6.SetLabel('姓名:')
        self.text7.SetLabel('學號:')
        self.text8.SetLabel('學院:')
        self.grid.ff()
        self.grid.Update()
        self.text5.SetLabel(u'\n溫馨提示:\n' + '⚪登錄信息驗證中...')
        self.text5.Update()
        try:
            name = self.text2.GetValue()
            password = self.text4.GetValue()
            courses = get_data_from_zsc1(name,password)
            if courses.result == False:
                self.text5.SetLabel(u'\n溫馨提示:\n' + '×用戶名或密碼錯誤!' )
            else:
                student_course = courses.my_score_list
                self.text5.SetLabel(u'\n溫馨提示:\n' + '√登錄成功並獲得成績!')
                self.text6.SetLabel('姓名:' + courses.my_information[0])
                self.text7.SetLabel('學號:' + courses.my_information[1])
                self.text8.SetLabel('學院:' + courses.my_information[2] )
                try:
                    self.grid.header = grade
                    for i in range(len(self.grid.header)):
                        self.grid.SetColLabelValue(i, self.grid.header[i])
                    self.grid.Update()
                    self.grid.Refresh()
                except:
                    pass
                n1 = len(student_course)
                n2 = len(student_course[0])
                self.grid.InsertRows(0, n1)
                for i in range(0, n1):
                    for j in range(0, n2):
                        self.grid.SetCellValue(i, j, student_course[i][j])
                #self.grid.AutoSize() #自動調整單元格大小
        except:
            self.Message = wx.MessageDialog(self, '賬號或密碼不能爲空!', 'ERROR',wx.ICON_ERROR)
            self.Message.ShowModal()
            self.Message.Destroy()

    def command4_event(self, event):
        self.text6.SetLabel('姓名:')
        self.text7.SetLabel('學號:')
        self.text8.SetLabel('學院:')
        self.grid.ff()
        self.grid.Update()
        self.text5.SetLabel(u'\n溫馨提示:\n' + '⚪登錄信息驗證中...')
        self.text5.Update()
        try:
            name = self.text2.GetValue()
            password = self.text4.GetValue()
            courses = get_data_from_zsc2(name,password)
            if courses.result == False:
                self.text5.SetLabel(u'\n溫馨提示:\n' + '×用戶名或密碼錯誤!' )
            else:
                student_timetable = courses.all_data
                self.text5.SetLabel(u'\n溫馨提示:\n' + '√登錄成功並獲得課程表!')
                self.text6.SetLabel('姓名:' + courses.my_information[0])
                self.text7.SetLabel('學號:' + courses.my_information[1])
                self.text8.SetLabel('學院:' + courses.my_information[2] )
                try:
                    self.grid.header = timetable
                    for i in range(len(self.grid.header)):
                        self.grid.SetColLabelValue(i, self.grid.header[i])
                    self.grid.Update()
                    self.grid.Refresh()
                except:
                    pass
                n1 = len(student_timetable)
                n2 = len(student_timetable[0])
                self.grid.InsertRows(0, n1)
                for i in range(0, n1):
                    for j in range(0, n2):
                        self.grid.SetCellValue(i, j, student_timetable[i][j])
                self.grid.SetCellSize(6, 0, 1, 5) #合併單元格 合併第6行第1個爲1x5的單元格
                #設置行的高度
                for i in range(0, n1):
                    self.grid.SetRowSize(i, 80)
                #設置列的寬度
                for j in range(0, n2):
                    self.grid.SetColSize(j, 120)
                #self.grid.AutoSize()
        except:
            self.Message = wx.MessageDialog(self, '賬號或密碼不能爲空!', 'ERROR',wx.ICON_ERROR)
            self.Message.ShowModal()
            self.Message.Destroy()
    def command3_event(self, event):
        self.Message = wx.MessageDialog(self, '對不起,此功能暫時未開放!', 'ERROR', wx.ICON_ERROR)
        self.Message.ShowModal()
        self.Message.Destroy()
    def command2_event(self, event):
        self.text2.SetValue('')
        self.text4.SetValue('')
        self.text5.SetLabel('\n\n')
        self.text6.SetLabel('姓名:')
        self.text7.SetLabel('學號:')
        self.text8.SetLabel('學院:')
        self.grid.ff()
        self.grid.Update()

class mygrid(wx.grid.Grid):
    def __init__(self, parent):
        wx.grid.Grid.__init__(self, parent=parent, id=-1)
        #self.CreateGrid(0, 7)
        self.SetDefaultCellBackgroundColour('#BFD8D8')#parent.BackgroundColour
        self.SetDefaultCellTextColour("#000000")
        self.CreateGrid(0, 13)
        for i in range(13):
            self.SetColLabelValue(i,'列名')
        self.header = []
    def ff(self):
        self.ClearGrid()
        if self.GetNumberRows()>0:
            self.DeleteRows(0, self.GetNumberRows())
        #self.DeleteCols(0, self.GetNumberCols())
        for i in range(13):
            self.SetColLabelValue(i, '列名')
        self.Update()
        self.Refresh()
        self.header = []


class get_data_from_zsc1():
    def __init__(self, userAccount, password):
        driver_path = r'E:\py\chromedriver\chromedriver.exe'
        chrome_options = Options()
        chrome_options.add_argument('--headless')
        chrome_options.add_argument('--disable-gpu')

        driver = webdriver.Chrome(executable_path=driver_path, chrome_options=chrome_options)

        driver.get('http://jwgln.zsc.edu.cn/jsxsd/')
        driver.implicitly_wait(1)
        driver.find_element_by_id("userAccount").send_keys(userAccount)
        driver.find_element_by_id("userPassword").send_keys(password)
        driver.find_element_by_xpath('//*[@id="btnSubmit"]').click()  # 用click模擬瀏覽器點擊
        driver.implicitly_wait(1)
        self.my_information = []
        self.my_score_list = []
        self.result = self.if_or_get_data(driver)
    def if_or_get_data(self,driver):
        if '用戶名或密碼錯誤' in driver.page_source:
            driver.close()
            driver.quit()
            return False
        else:
            driver1 = self.get_course1(driver)
            self.get_course2(driver1)
            return True
    def get_course1(self,driver):
        #獲取姓名,學號
        bs = BeautifulSoup(driver.page_source, 'lxml')
        information = list(bs.find('div',attrs ={'class':"block1text"}))
        self.my_information.append(information[0].split(":")[1])
        self.my_information.append(information[2].split(":")[1])
        #獲取成績
        driver.find_element_by_xpath('//*[@class="block7"]').click()
        driver.implicitly_wait(1)
        driver.find_element_by_xpath('//*[@id="btn_query"]').click()
        bs = BeautifulSoup(driver.page_source, 'lxml')
        my_score_detail = bs.find_all(name='td')[1:]
        my_score_detail = list(my_score_detail)
        score_list = [i.string for i in my_score_detail]
        for i in range(0, len(score_list), 14):
            course_list = []
            for j in range(i+1, i + 14):
                if score_list[j]== None:
                    course_list.append('\n')
                else:
                    course_list.append(score_list[j])
            self.my_score_list.append(course_list)
        return  driver
    #獲得學院
    def get_course2(self, driver):
        driver.find_element_by_xpath('//*[@title="培養管理"]').click()
        driver.implicitly_wait(1)
        driver.find_element_by_xpath('//*[@href="/jsxsd/pyfa/pyfa_query"]').click()
        # 用bs4進行數據篩選
        bs = BeautifulSoup(driver.page_source, 'lxml')
        my_score_detail = bs.find_all(name='td', attrs={'align': "left"})
        my_score_detail = list(my_score_detail)
        my_score_list = [i.string for i in my_score_detail]
        Map = dict()
        for i in range(len(my_score_list)):
            if i % 4 == 3:
                if my_score_list[i] not in Map.keys():
                    Map[my_score_list[i]] = 0
                else:
                    Map[my_score_list[i]] = Map[my_score_list[i]] + 1
        college = max(Map.keys(), key=(lambda x: Map[x]))
        self.my_information.append(college)
        driver.close()
        driver.quit()

class get_data_from_zsc2():
    def __init__(self, userAccount, password):
        driver_path = r'E:\py\chromedriver\chromedriver.exe'
        chrome_options = Options()
        chrome_options.add_argument('--headless')
        chrome_options.add_argument('--disable-gpu')

        driver = webdriver.Chrome(executable_path=driver_path, chrome_options=chrome_options)
        self.all_data = []
        driver.get('http://jwgln.zsc.edu.cn/jsxsd/')
        driver.implicitly_wait(1)
        driver.find_element_by_id("userAccount").send_keys(userAccount)
        driver.find_element_by_id("userPassword").send_keys(password)
        driver.find_element_by_xpath('//*[@id="btnSubmit"]').click()  # 用click模擬瀏覽器點擊
        driver.implicitly_wait(1)
        self.my_information = []
        self.my_score_list = []
        self.result = self.if_or_get_data(driver)
    def if_or_get_data(self,driver):
        if '用戶名或密碼錯誤' in driver.page_source:
            driver.close()
            driver.quit()
            return False
        else:
            driver1 = self.get_course1(driver)
            self.get_course2(driver1)
            return True
    def get_course1(self,driver):
        #獲取姓名,學號
        bs = BeautifulSoup(driver.page_source, 'lxml')
        information = list(bs.find('div',attrs ={'class':"block1text"}))
        self.my_information.append(information[0].split(":")[1])
        self.my_information.append(information[2].split(":")[1])
        #獲取個人課程表
        driver.find_element_by_xpath('//*[@class="block4"]').click()
        driver.implicitly_wait(1)
        driver.find_element_by_xpath('//*[@href="/jsxsd/xskb/xskb_list.do"]').click()
        time.sleep(1)
        #頁面彈窗點擊確認
        try:
            alt = driver.switch_to_alert()
            alt.accept()
        except:
            pass
        try:
            Select(driver.find_element_by_id("xnxq01id")).select_by_value("2019-2020-2").click()
        except:
            pass
        soup = BeautifulSoup(driver.page_source, 'lxml')
        page = soup.find_all('div', attrs={'class': "kbcontent"})
        teachers1, teachers2 = [], []
        weeks1, weeks2 = [], []
        classrooms1, classrooms2 = [], []
        for i in page:
            teachers1.append(i.find('font', attrs={'title': '老師'}))
            weeks1.append(i.find('font', attrs={'title': '周次(節次)'}))
            classrooms1.append(i.find('font', attrs={'title': '教室'}))
        my_detail = list(page)
        for i in teachers1:
            if i == None:
                teachers2.append('\n')
            else:
                teachers2.append(i.string)
        for i in weeks1:
            if i == None:
                weeks2.append('\n')
            else:
                weeks2.append('\n' + i.string)
        for i in classrooms1:
            if i == None:
                classrooms2.append('\n')
            else:
                classrooms2.append('\n' + i.string)
        pitch_number = ['(上午)\n第1,2節\n(08:00-08:45)\n(08:55-09:40)', '(上午)\n第3,4節\n(10:00-10:45)\n(10:55-11:40)',
                        '(下午)\n第5,6節\n(14:30-15:15)\n(15:25-16:10)', '(下午)\n第7,8節\n(16:20-16:05)\n(17:15-18:00)',
                        '(晚上)\n第9,10節\n(19:30-20:15)\n(20:25-21:10)', '第11,12節', '第13,14節']
        temp = []
        temp.append(pitch_number[0])
        num = 0
        pnum = 0
        for i in range(len(my_detail)):
            if my_detail[i].text == '\xa0':
                temp.append('\n\n\n')
            else:
                temp.append(my_detail[i].text.split(teachers2[i])[0] + '\n' + teachers2[i] + weeks2[i] + classrooms2[i])
            num = num + 1
            if num == 7:
                self.all_data.append(temp)
                temp = []
                pnum = pnum + 1
                temp.append(pitch_number[pnum])
                num = 0
        page2 = soup.find('td', attrs={'colspan': "7"})
        BZ = ['備註:' + page2.text, '\n', '\n', '\n', '\n', '\n', '\n', '\n']
        self.all_data.append(BZ)
        return  driver
    #獲得學院
    def get_course2(self, driver):
        driver.find_element_by_xpath('//*[@title="培養管理"]').click()
        driver.implicitly_wait(1)
        driver.find_element_by_xpath('//*[@href="/jsxsd/pyfa/pyfa_query"]').click()
        # 用bs4進行數據篩選
        bs = BeautifulSoup(driver.page_source, 'lxml')
        my_score_detail = bs.find_all(name='td', attrs={'align': "left"})
        my_score_detail = list(my_score_detail)
        my_score_list = [i.string for i in my_score_detail]
        Map = dict()
        for i in range(len(my_score_list)):
            if i % 4 == 3:
                if my_score_list[i] not in Map.keys():
                    Map[my_score_list[i]] = 0
                else:
                    Map[my_score_list[i]] = Map[my_score_list[i]] + 1
        college = max(Map.keys(), key=(lambda x: Map[x]))
        self.my_information.append(college)
        driver.close()
        driver.quit()
if __name__ == '__main__':
    app = wx.App()
    frame = myFrame()
    app.MainLoop()
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章