NoColor's Blog - index
type: channel
link: https://www.nocolor.cc/tag/index/
items: 爬取URP教务系统课表并生成ics日历文件

爬取URP教务系统课表并生成ics日历文件
type: item
date: 2020-08-07T22:43:00+08:00
description: 前言最近用Python写了一个工具，可以把课程表导出到iCalendar格式，之后可以把这个文件导入到系统的软件当中，比如iPhone自带的日历, Outlook, Google Calendar 等等你常用的日历工具，使得提醒更加明朗。先上图，这是将课表导入Google日历的样子，省去繁琐的教务处课表查询/专属app广告，甚至可以在上课前30分钟提醒（再也不用担心迟到了）。适用范围本人测试环境为河北工业大学，理论上新版URP教务系统均可使用。使用方法先上源码，最后面有下载链接。import requests import re import datetime import configparser import hashlib import json import os def md5(str): m = hashlib.md5() m.update(str.encode("utf8")) return m.hexdigest() config = configparser.ConfigParser() config.read('config.ini') # 教务系统地址 url = config['URP'].get('url') username = input('id:') password = input('password:') session = requests.session() im_url = url + 'img/captcha.jpg' im_data = session.get(im_url) # 验证码处理及模拟登陆 with open('captcha.jpg', 'wb') as file: file.write(im_data.content) captcha = input("Captcha(saved in the code directory):") login_url = url + 'j_spring_security_check' post_data = { 'j_username': username, 'j_password': md5(password), 'j_captcha': captcha } # 课表数据获取 login_res = session.post(login_url, data=post_data) table_url = url + 'student/courseSelect/thisSemesterCurriculum/ajaxStudentSchedule/curr/callback' tablePage = session.get(table_url).text table_byte = bytes(tablePage, 'utf-8') # 数据预处理 with open('class.json', 'wb') as file: file.write(table_byte) file.close() with open('class.json', "a+", encoding='utf-8') as f: old = f.read() f.seek(0) f.write(']') f.close() with open('class.json', "r+", encoding='utf-8') as f: old = f.read() f.seek(0) f.write('[') f.write(old) # 写入日历 # 第一周周一日期 startYear = config['startDate'].getint('year') startMonth = config['startDate'].getint('month') startDay = config['startDate'].getint('day') beginDate = datetime.date(startYear, startMonth, startDay) startTime = [None] startTime.extend(config['time'].get('startTime').replace(' ', '').split(',')) endTime = [None] endTime.extend(config['time'].get('endTime').replace(' ', '').split(',')) weekName = [None] weekName.extend(config['time'].get('weekName').replace(' ', '').split(',')) VCALENDAR = '''BEGIN:VCALENDA…