原理:爬虫爬取课表,进行正则匹配生成对应ics文件
实在不会可联系本人
效果
集美版本
注意:first_day_date_str = '2023-02-13'
需要修改为当前学期的第一天
并且需要打开i集大课表进行复制课程信息到一个txt文件重命名为data1.json然后方可运行
获取信息步骤如下(如遇不会请联系本来,博客内有联系方式)
# -*- coding:utf-8 -*-
import requests
from icalendar import Calendar, Event
import pytz
import datetime
import hashlib
import os
import re
# 支持json
import json
# 返回上课的日期
def get_course_date(first_day_date_str, course_in_day_of_week, which_week):
first_day_date = datetime.datetime.strptime(
first_day_date_str, r'%Y-%m-%d')
return first_day_date + datetime.timedelta(days=(which_week - 1) * 7 + (course_in_day_of_week - 1))
# 返回上课时间
def get_course_take_time(course_time):
# 第1、2节课8:0:0 - 9:35:0 以此类推。
course_time_table = {'12': [[8, 0, 0], [9, 35, 0]],
'34': [[10, 5, 0], [11, 40, 0]],
'14': [[8, 0, 0], [11, 40, 0]],
'56': [[14, 0, 0], [15, 35, 0]],
'78': [[15, 55, 0], [17, 30, 0]],
'58': [[14, 0, 0], [17, 30, 0]],
'910': [[19, 0, 0], [20, 35, 0]],
'911': [[19, 0, 0], [21, 25, 0]]}
return course_time_table[course_time][0], course_time_table[course_time][1]
def getInformation(json_data):
return json_data["studentTableVm"]["name"], json_data["studentTableVm"]["id"], json_data["studentTableVm"]["grade"], \
json_data["studentTableVm"]["major"], json_data["studentTableVm"]["adminclass"]
def main():
first_day_date_str = '2023-02-13'
time_zone = pytz.timezone('Asia/Shanghai')
# 保存日历信息
calendar = Calendar()
# 初始化?
calendar.add('prodid', '-//My calendar product//mxm.dk//')
calendar.add('version', '2.0')
with open('data1.json', encoding='utf-8') as f:
json_data = json.load(f)
name, studentId, grade, major, class_name = getInformation(json_data)
courseList = json_data["studentTableVm"]["activities"]
print(type(courseList))
#print(courseList)
for course in courseList:
# 课程在星期几
course_in_day_of_week = int(course['weekday'])
# 课程在第几周(列表形式)
course_take_weeks = list(course['weekIndexes'])
# 课程开始时间、结束时间
course_begin_time, course_end_time = get_course_take_time(
"{}{}".format(course['startUnit'], course['endUnit']))
# 遍历上课周次
for week in course_take_weeks:
course_date = get_course_date(first_day_date_str, course_in_day_of_week, week)
event = Event()
event.add('summary', course['courseName'])
event.add('dtstart',
datetime.datetime(course_date.year, course_date.month, course_date.day, course_begin_time[0],
course_begin_time[1], course_begin_time[2], tzinfo=time_zone))
event.add('dtend',
datetime.datetime(course_date.year, course_date.month, course_date.day, course_end_time[0],
course_end_time[1], course_end_time[2], tzinfo=time_zone))
event.add('dtstamp',
datetime.datetime(course_date.year, course_date.month, course_date.day, course_begin_time[0],
course_begin_time[1], course_begin_time[2], tzinfo=time_zone))
event.add('location', course['room'])
calendar.add_component(event)
output_file_name = '{} - {}.ics'.format(class_name, name)
output_file = open(output_file_name, 'wb')
output_file.write(calendar.to_ical())
output_file.close()
if __name__ == '__main__':
main()
cookie获取方式为打开课程表页面按F12点网络,随便随便点一个出现的东西,里面有个cookie复制下来就行,
不会可联系本人下载
注意:first_day_date_str = '2023-02-13'
需要改为当前学期第一天的日期
import re
import requests
from lxml import etree
from icalendar import Calendar, Event
import pytz
import datetime
#cookie=input("请输入你的cookie:")
cookie="_sp_id.acfd=816ef727-feba-45dc-9088-1cf6bee65d7a.668868858.1.168868858.668868858.1e1e3108-ce79-4096-9f6b-bc35e5446a5c; ASP.NET_SessionId=z1sejm551jdpmd55ve35v545"
#这里cookie要换为自己的
# 返回上课时间
def get_course_take_time(course_time):
# 第1、2节课8:0:0 - 9:35:0 以此类推。
course_time_table = {'12': [[8, 0, 0], [9, 35, 0]],
'34': [[10, 5, 0], [11, 40, 0]],
'14': [[8, 0, 0], [11, 40, 0]],
'56': [[14, 0, 0], [15, 35, 0]],
'78': [[15, 55, 0], [17, 30, 0]],
'58': [[14, 0, 0], [17, 30, 0]],
'910': [[19, 0, 0], [20, 35, 0]],
'911': [[19, 0, 0], [21, 25, 0]]}
return course_time_table[course_time][0], course_time_table[course_time][1]
# 返回上课的日期
def get_course_date(first_day_date_str, course_in_day_of_week, which_week):
first_day_date = datetime.datetime.strptime(
first_day_date_str, r'%Y-%m-%d')
return first_day_date + datetime.timedelta(days=(which_week - 1) * 7 + (course_in_day_of_week - 1))
def get_course(st,week,x):
courses=st.split("★")
# print(courses)
course_name=""
course_location=""
course_teacher=""
course_week=""
#print(len(courses))
#print(courses)
if len(courses)>=3:
for course in courses:
if course!="":
course=course.split(" ")
if len(course)==5:
#print(course)
course_name=course[0]
course_location=course[2]
course_teacher=course[3]
course_week=course[4]
elif len(course)==3:
course_name = course[0]
course_location = " "
course_teacher = course[1]
course_week = course[2]
elif len(course)==2:
course_name=course[0]
course_location=" "
course_teacher=" "
course_week = course[1]
print("星期",x,",""第", 2 * week - 3, "--", 2 * week - 2, "节:", end="\t")
print(course_name, course_location, course_teacher, course_week)
#print(course_week)
start = 0
end = 0
#print(course_week[-1:])
if (str(course_week)[-1:] == "周"):
start = end = re.search('\d+', course_week).group()
print("start:", start, "end:", end)
pa = '\d+-\d+'
go_week = re.search(pa, course_week)
if go_week!=None:
#print(go_week.group())
start=go_week.group().split("-")[0]
end=go_week.group().split("-")[1]
print("start:",start,"end:",end)
# 课程在星期几
course_in_day_of_week = int(x)
# 课程在第几周(列表形式)
course_take_weeks = list(range(int(start),int(end)+1))
# 课程开始时间、结束时间
course_begin_time, course_end_time = get_course_take_time(
"{}{}".format(2*week-3, 2*week-2)) # 12表示早上第一二节
for week1 in course_take_weeks:
course_date = get_course_date(first_day_date_str, course_in_day_of_week, week1)
event = Event()
event.add('summary', course_name)
event.add('dtstart',
datetime.datetime(course_date.year, course_date.month, course_date.day, course_begin_time[0],
course_begin_time[1], course_begin_time[2], tzinfo=time_zone))
event.add('dtend',
datetime.datetime(course_date.year, course_date.month, course_date.day, course_end_time[0],
course_end_time[1], course_end_time[2], tzinfo=time_zone))
event.add('dtstamp',
datetime.datetime(course_date.year, course_date.month, course_date.day, course_begin_time[0],
course_begin_time[1], course_begin_time[2], tzinfo=time_zone))
event.add('location', course_location)
calendar.add_component(event)
output_file_name = '{} - {}.ics'.format(class_name, name)
output_file = open(output_file_name, 'wb')
output_file.write(calendar.to_ical())
output_file.close()
else:
for course in courses:
if course != "":
course = course.split(" ")
#print(len(course))
#print(course)
if len(course) == 5:
# print(course)
course_name = course[0]
course_location = course[2]
course_teacher = course[3]
course_week = course[4]
elif len(course) == 3:
#print(course)
course_name = course[0]
course_location = " "
course_teacher = course[1]
course_week = course[2]
elif len(course)==2:
course_name=course[0]
course_location=" "
course_teacher=" "
course_week = course[1]
print("星期",x,",""第", 2 * week - 3, "--", 2 * week - 2, "节:", end="\t")
print(course_name, course_location, course_teacher, course_week)
#print(course_week)
start = 0
end = 0
#print(course_week[-1:])
if (str(course_week)[-1:] == "周"):
start = end = re.search('\d+', course_week).group()
print("start:", start, "end:", end)
pa='\d+-\d+'
go_week = re.search(pa, course_week)
if go_week != None:
# print(go_week.group())
start = go_week.group().split("-")[0]
end = go_week.group().split("-")[1]
print("start:", start, "end:", end)
# 课程在星期几
course_in_day_of_week = int(x)
# 课程在第几周(列表形式)
course_take_weeks = list(range(int(start), int(end) + 1))
# 课程开始时间、结束时间
course_begin_time, course_end_time = get_course_take_time(
"{}{}".format(2*week-3, 2*week-2)) # 12表示早上第一二节
for week1 in course_take_weeks:
course_date = get_course_date(first_day_date_str, course_in_day_of_week, week1)
event = Event()
event.add('summary', course_name)
event.add('dtstart',
datetime.datetime(course_date.year, course_date.month, course_date.day,
course_begin_time[0],
course_begin_time[1], course_begin_time[2], tzinfo=time_zone))
event.add('dtend',
datetime.datetime(course_date.year, course_date.month, course_date.day,
course_end_time[0],
course_end_time[1], course_end_time[2], tzinfo=time_zone))
event.add('dtstamp',
datetime.datetime(course_date.year, course_date.month, course_date.day,
course_begin_time[0],
course_begin_time[1], course_begin_time[2], tzinfo=time_zone))
event.add('location', course_location)
calendar.add_component(event)
output_file_name = '{} - {}.ics'.format(class_name, name)
output_file = open(output_file_name, 'wb')
output_file.write(calendar.to_ical())
output_file.close()
url="http://cyjwgl.jmu.edu.cn/Student/ViewMyStuSchedule.aspx"
head={
"Cookie":cookie,
"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.82 Safari/537.36"
}
response=requests.get(url=url,headers=head)
tree = etree.HTML(response.text)
title = tree.xpath('//*[@id="ctl00_ContentPlaceHolder1_lbtitle"]/text()')
#初始化课表
first_day_date_str = '2023-02-13'
time_zone = pytz.timezone('Asia/Sha'
'nghai')
# 保存日历信息
calendar = Calendar()
# 初始化?
calendar.add('prodid', '-//My calendar product//mxm.dk//')
calendar.add('version', '2.0')
name=title
class_name =""
print(title)
for week in range(2,7):
body = tree.xpath(f'//*[@id="ctl00_ContentPlaceHolder3_ScheduleTable"]//tr[{week}]')
for i in body:
for x in range(1,8):
#print(x)#表示周几
#print(i.xpath("./td/text()")[x])
st=""
for a in i.xpath("./td/text()")[x]:
st+=str(a)
#print(st,"weekday:",week-1)
if st!=" ":
# print(st,"weekday:",week-1)
# print(st)
# print("第",2*week-3,"--",2*week-2,"节:",end="\t")
get_course(st,week,x)
赞赏
微信赞赏 支付宝赞赏