前言
今天就寫寫在之前用模擬登錄爬取教務系統的成績單的代碼基礎上,用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()
具體的界面各方面從設置佈局開始,分爲兩個佈局。一個是登錄界面佈局
另一個是獲取的數據用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()