当前仓库属于暂停状态,部分功能使用受限,详情请查阅 仓库状态说明
1 Star 0 Fork 2

luenay/flask_dengji
暂停

forked from ghc/flask_dengji
暂停
 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
kebiao.py 13.61 KB
一键复制 编辑 原始数据 按行查看 历史
ghc 提交于 2019-12-31 18:21 . 12.31 更新2048排行榜
import time
from datetime import datetime
import numpy as np
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import Select
from os.path import exists
from os import remove
import zipfile
import os
import pandas as pd
from openpyxl import load_workbook
from openpyxl.styles import Alignment, Border, Side
import re
class ChaXun(object):
def __init__(self):
if exists(r'C:\Users\Administrator\Downloads\场地课表[刘志铭].zip'):
remove(r'C:\Users\Administrator\Downloads\场地课表[刘志铭].zip')
driver_path = './chromedriver.exe'
self.base_url = 'http://1.85.30.53/jwglxt/xtgl/login_slogin.html?language=zh_CN&_t=1542526075759'
self.user_name = '2019030901'
self.password = 'jsjzx@51101'
option = webdriver.ChromeOptions()
# 禁止出现浏览器上方出现 ‘Chrome正在受到自动软件的控制’
option.add_argument('disable-infobars')
# 直接默认最大化窗口打开
option.add_argument('--start-maximized')
# 更改屏幕分辨率
option.add_argument('--window-size=1920,10800')
# self.workbook = xlwt.Workbook(encoding='utf-8')
# self.worksheet1 = self.workbook.add_sheet('调进')
# self.worksheet2 = self.workbook.add_sheet('调出')
# self.style = xlwt.XFStyle() # 初始化样式
# font = xlwt.Font() # 为样式创建字体
# font.name = 'Times New Roman'
# font.bold = True # 黑体
# self.style.font = font # 设定样式
# option.add_argument('--headless')
self.driver = webdriver.Chrome(options=option, executable_path=driver_path)
self.driver.implicitly_wait(10)
def main(self):
driver = self.driver
driver.get(self.base_url)
driver.find_element_by_xpath('//input[@id="yhm"]').send_keys(self.user_name)
driver.find_element_by_xpath('//input[@id="mm"]').send_keys(self.password)
driver.find_element_by_xpath('//button[@id="dl"]').click()
time.sleep(2)
# 右上角头像
driver.find_element_by_xpath('//a[@data-toggle="dropdown"]').click()
# 角色切换
jiaoseqiehuan = driver.find_element_by_xpath('//li[@class="open"]/ul[@class="dropdown-menu"]/li[1]/a')
# 鼠标悬停
ActionChains(driver).move_to_element(jiaoseqiehuan).perform()
time.sleep(2)
driver.find_element_by_link_text('教师').click()
time.sleep(2)
driver.find_element_by_link_text('课表打印').click()
time.sleep(2)
driver.find_element_by_link_text('场地课表打印').click()
driver.switch_to.window(driver.window_handles[-1])
time.sleep(1)
# 选择本部
driver.find_element_by_xpath('//div[@id="xqh_id_chosen"]/a[@class="chosen-single"]').click()
time.sleep(3)
driver.find_element_by_xpath('//div[@id="xqh_id_chosen"]/div/ul/li[@data-option-array-index="1"]').click()
# 选择明远楼
driver.find_element_by_xpath('//div[@id="lh_chosen"]/a[@class="chosen-single"]').click()
time.sleep(3)
driver.find_element_by_xpath('//div[@id="lh_chosen"]/div/ul/li[@data-option-array-index="3"]').click()
# 一页显示多少条
selector = Select(driver.find_element_by_xpath('//select[@class="ui-pg-selbox"]'))
selector.select_by_value('50')
# 开始查询
driver.find_element_by_xpath('//button[@id="search_go"]').click()
time.sleep(2)
js1 = "document.documentElement.scrollTop=10000"
driver.execute_script(js1)
js = "document.documentElement.scrollTop=0"
driver.execute_script(js)
driver.find_element_by_xpath('//td[contains(text(),"131102")]/preceding-sibling::td').click()
time.sleep(0.5)
driver.find_element_by_xpath('//td[contains(text(),"131108")]/preceding-sibling::td').click()
time.sleep(0.5)
driver.find_element_by_xpath('//td[contains(text(),"131112")]/preceding-sibling::td').click()
time.sleep(0.5)
driver.find_element_by_xpath('//td[contains(text(),"131113")]/preceding-sibling::td').click()
time.sleep(0.5)
driver.find_element_by_xpath('//td[contains(text(),"131206")]/preceding-sibling::td').click()
time.sleep(0.5)
driver.find_element_by_xpath('//td[contains(text(),"131114")]/preceding-sibling::td').click()
time.sleep(0.5)
driver.find_element_by_xpath('//td[contains(text(),"131210")]/preceding-sibling::td').click()
time.sleep(0.5)
driver.find_element_by_xpath('//td[contains(text(),"131211")]/preceding-sibling::td').click()
time.sleep(0.5)
driver.find_element_by_xpath('//td[contains(text(),"131214")]/preceding-sibling::td').click()
time.sleep(0.5)
driver.find_element_by_xpath('//td[contains(text(),"131306")]/preceding-sibling::td').click()
time.sleep(0.5)
# 为了点击到1201
js1 = "document.documentElement.scrollTop=10000"
driver.execute_script(js1)
time.sleep(0.5)
driver.find_element_by_xpath('//td[contains(text(),"131201")]/preceding-sibling::td/input').click()
time.sleep(0.5)
driver.find_element_by_xpath('//button[@onclick="batchShcExcel()"]').click()
time.sleep(10)
driver.close()
driver.quit()
os.system('taskkill /im chromedriver.exe /F')
os.system('taskkill /im chrome.exe /F')
k = ChaXun()
week = datetime.now().isocalendar()[1] + 18
try:
k.main()
f = zipfile.ZipFile(r'C:\Users\Administrator\Downloads\场地课表[刘志铭].zip', 'r')
if not os.path.exists('./class_table'):
os.mkdir('./class_table')
for file in os.listdir('./class_table'):
os.remove('./class_table/' + file)
f.extractall('./class_table')
f.close()
tablelst = os.listdir('./class_table')
# 直接to_excel无法将数据插入同一xlsx内的不同sheet
writer = pd.ExcelWriter('课表.xlsx')
# 格式化表格数据
for t in tablelst:
df = pd.read_excel('./class_table/' + t, header=1, skipfooter=1, index_col=[0, 1])
df.index.set_names(['节次', '时间'], inplace=True)
df.columns.set_names([t[-8:-4]], inplace=True)
# 清理不在当前周数的课程或已过期的过程信息
for zhouci in ['星期一', '星期二', '星期三', '星期四', '星期五']:
for jieci in ['第一二节', '第三四节']:
# 如果当前课程不为空
if pd.notna(df[zhouci]['上午'][jieci]):
# 将多门课程信息切分成列表
infol = df[zhouci]['上午'][jieci].split('\r\n') # type:list
# 循环判断每个课程信息
flag1 = True
# info为每个课程信息 循环判断每个信息 如果有符合 直接跳出循环
for info in infol:
# 抓取到课程上课周次 抓区间
w = re.findall('(\d+?)-(\d+?)周', info.split('/')[1])
# 如果抓取不到 则是单周上课 如13周
if len(w) == 0:
w1 = re.findall('(\d+?)周', info.split('/')[1]) # type:list
# 多个单周 如5周,8周,13周
if str(week) not in w1:
df[zhouci]['上午'][jieci] = np.nan
else:
df[zhouci]['上午'][jieci] = info
flag1 = False
# 抓取到了1个 就是有一个周次区间 如1-16周
if len(w) == 1:
if int(w[0][0]) > week or int(w[0][1]) < week:
df[zhouci]['上午'][jieci] = np.nan
else:
df[zhouci]['上午'][jieci] = info
flag1 = False
# 抓取到了多个 就是有多个周次区间 如 3-10周,12-14周(双),15-16周
elif len(w) > 1:
flag = True
# 循环判断每个区间 如果有所有区间在都不在本周 flag应该是False
for ww in w:
if int(ww[0]) <= week <= int(ww[1]):
flag = True
break
elif int(ww[0]) > week or int(ww[1]) < week:
flag = False
if not flag:
df[zhouci]['上午'][jieci] = np.nan
else:
df[zhouci]['上午'][jieci] = info
flag1 = False
if not flag1:
break
for jieci in ['第五六节', '第七八节']:
if pd.notna(df[zhouci]['下午'][jieci]):
# if 'SQLsever/7周/刘彧/智能交通1822;智能交通1823/教学班人数:98' in df[zhouci]['下午'][jieci]:
# df[zhouci]['下午'][jieci] = df[zhouci]['下午'][jieci].split('\r\n')[0]
infol = df[zhouci]['下午'][jieci].split('\r\n') # type:list
flag1 = True
for info in infol:
w = re.findall('(\d+?)-(\d+?)周', info.split('/')[1])
if len(w) == 0:
w1 = re.findall('(\d+?)周', info.split('/')[1])
if str(week) not in w1:
df[zhouci]['下午'][jieci] = np.nan
else:
df[zhouci]['下午'][jieci] = info
flag1 = False
if len(w) == 1:
if int(w[0][0]) > week or int(w[0][1]) < week:
df[zhouci]['下午'][jieci] = np.nan
else:
df[zhouci]['下午'][jieci] = info
flag1 = False
elif len(w) > 1:
flag = True
for ww in w:
if int(ww[0]) <= week <= int(ww[1]):
flag = True
break
elif int(ww[0]) > week or int(ww[1]) < week:
flag = False
if not flag:
df[zhouci]['下午'][jieci] = np.nan
else:
df[zhouci]['下午'][jieci] = info
flag1 = False
if not flag1:
break
if pd.notna(df[zhouci]['晚上']['第九十节']):
infol = df[zhouci]['晚上']['第九十节'].split('\r\n') # type:list
flag1 = True
for info in infol:
w = re.findall('(\d+?)-(\d+?)周', info.split('/')[1])
if len(w) == 0:
w1 = re.findall('(\d+?)周', info.split('/')[1])
if str(week) not in w1:
df[zhouci]['晚上']['第九十节'] = np.nan
else:
df[zhouci]['晚上']['第九十节'] = info
flag1 = False
if len(w) == 1:
if int(w[0][0]) > week or int(w[0][1]) < week:
df[zhouci]['晚上']['第九十节'] = np.nan
else:
df[zhouci]['晚上']['第九十节'] = info
flag1 = False
elif len(w) > 1:
flag = True
for ww in w:
if int(ww[0]) <= week <= int(ww[1]):
flag = True
break
elif int(ww[0]) > week or int(ww[1]) < week:
flag = False
if not flag:
df[zhouci]['晚上']['第九十节'] = np.nan
else:
df[zhouci]['晚上']['第九十节'] = info
flag1 = False
if not flag1:
break
df.to_excel(writer, sheet_name=t[-8:-4])
# 存入同一文件内
writer.save()
border = Border(left=Side(border_style='thin', color='000000'),
right=Side(border_style='thin', color='000000'),
top=Side(border_style='thin', color='000000'),
bottom=Side(border_style='thin', color='000000'))
wb = load_workbook('课表.xlsx')
sheetNameList = wb.sheetnames
for sheetName in sheetNameList:
ws = wb[sheetName]
for i in ['C', 'D', 'E', 'F', 'G']:
for j in range(2, 7):
ws[i + str(j)].alignment = Alignment(horizontal='center', vertical='center', wrap_text=True)
ws[i + str(j)].border = border
ws.row_dimensions[j].height = 50
ws.column_dimensions[i].width = 30.0
wb.save('课表.xlsx')
print('教室课表更新完成')
except BaseException as e:
print('更新教室课表出错 错误%s' % e)
k.driver.quit()
os.system('taskkill /im chromedriver.exe /F')
os.system('taskkill /im chrome.exe /F')
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/luenay/flask_dengji.git
git@gitee.com:luenay/flask_dengji.git
luenay
flask_dengji
flask_dengji
master

搜索帮助