python3的wxpython界面模拟登录爬取教务系统成绩

前言

今天就写写在之前用模拟登录爬取教务系统的成绩单的代码基础上,用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.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.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()
            #调用模拟登录爬虫来爬取数据 get_data_from_zsc就是自己构造的类,用来爬取数据
            courses = get_data_from_zsc(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)
                #在grid表格对应的每行每列输入数据
                for i in range(0, n1):
                    for j in range(0, n2):
                        self.grid.SetCellValue(i, j, student_course[i][j])
        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()

具体的界面各方面从设置布局开始,分为两个布局。一个是登录界面布局
python3的wxpython界面模拟登录爬取教务系统成绩_第1张图片
另一个是获取的数据用wx.grid表格来可视化
在这里插入图片描述

然后是模拟登录获取数据的爬虫

具体的操作就不说了,之前的博文关于模拟登录教务系统有细说了,
此爬虫类主要是爬取学生姓名,学院,学号,以及成绩单

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()

最后是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’ 下载后更改储存图片的地址即可,图标资源就不提供了,太大了,就是圆标校徽
在这里插入图片描述

整体爬取成功的界面样子

在这里插入图片描述

最后的最后,奉上所有代码

将chromedriver.exe的存储位置改好,对应的库下载好,图片资源最好下载设置后位置,不弄的话将对应的设置图片代码行注释掉,应该也可以运行,只是会缺少相应的图片

# -*-coding:utf-8-*-
'''
@author Himit_ZH
Date:2020.01.20
'''
from selenium import webdriver # 从selenium导入webdriver
from selenium.webdriver.chrome.options import Options
from bs4 import BeautifulSoup
import wx
import wx.grid
import random
import time


sutuo = ['活动名称', '获得学分', '班级审核状态', '院系审核状态', '学校审核状态', '审核截止日期', '申报成功']
grade = ['开课学期', '课程编号', '课程名称', '总成绩', '学分',
                  '平时成绩', '期中成绩', '实验成绩', '期末成绩', '课程属性',
                  '课程性质', '备注', '考试性质']
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.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.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_zsc(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])
        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_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()
if __name__ == '__main__':
    app = wx.App()
    frame = myFrame()
    app.MainLoop()

你可能感兴趣的:(python3的wxpython界面模拟登录爬取教务系统成绩)