1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
| import requests import json import re
timeout = 20 session = requests.session()
def login(username, Password, lt): headers = { "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7", "Accept-Encoding": "gzip, deflate", "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6", "Cache-Control": "max-age=0", "Connection": "keep-alive", "Content-Length": "174", "Content-Type": "application/x-www-form-urlencoded", "Host": "authserver.squ.edu.cn", "Origin": "http://authserver.squ.edu.cn", "Referer": "http://authserver.squ.edu.cn/authserver/login?service=https%3A%2F%2Fjwgl.squ.edu.cn%2Fstudent%2Fsso%2Flogin", "Upgrade-Insecure-Requests": "1", "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36 Edg/138.0.0.0"} url = "http://authserver.squ.edu.cn/authserver/login" data = { "username": username, "password": Password, "rememberMe": "on", "_eventId": "submit", "dllt": "userNamePasswordLogin", "lt": lt, "execution": "e1s1", "rmShown": "11" } headers1 = { "Accept": "application/json", "method": "GET", "path": "/student/home/get-current-teach-week", "Sec-Ch-Ua": '"Not)A;Brand";v="8", "Chromium";v="138", "Microsoft Edge";v="138"', "Accept-Encoding": "gzip, deflate, br", "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8", "Cache-Control": "no-cache", "Connection": "keep-alive", "Pragma": "no-cache", "Sec-Fetch-Dest": "empty", "Sec-Fetch-Mode": "cors", "Sec-Fetch-Site": "same-origin", "User-Agent": "Mozilla/5.0 ", "Content-Type": "application/json", "origin": "https://jwgl.squ.edu.cn/student/home", "Referer": "https://jwgl.squ.edu.cn/student/home" } params = { 'service': 'https://jwgl.squ.edu.cn/student/sso/login?refer=https://jwgl.squ.edu.cn/student/home', } print("lt------>", lt) try: response1 = session.post(url, headers=headers, data=data, params=params, timeout=timeout) print(session.cookies.get_dict()) print(response1.url) response1.raise_for_status() print("第一次请求成功,使用密码登陆")
except requests.exceptions.Timeout: print(f"第一次请求超时(超过 {timeout} 秒)") except requests.exceptions.ConnectionError: print("第一次请求连接失败(可能是网络问题或域名错误)") except requests.exceptions.HTTPError as e: print("第一次请求连接失败") except Exception as e: print(f"第一次请求发生未知错误:{str(e)}")
try: response2 = session.get(response1.url, headers=headers1, timeout=timeout) print(session.cookies.get_dict()) print(response2.url) response2.raise_for_status() print("第二次请求成功") except requests.exceptions.Timeout: print(f"第二次请求超时(超过 {timeout} 秒)") except requests.exceptions.ConnectionError: print("第二次请求连接失败(可能是网络问题或域名错误)") except requests.exceptions.HTTPError as e: print("第二次请求连接失败") except Exception: print("第二次请求未知错误") try: a = session.cookies.get_dict()['__pstsid__'] b = session.cookies.get_dict()['SESSION'] url = "https://jwgl.squ.edu.cn/student/home" cookies = { "__pstsid__": a, "SESSION": b } except Exception as e: print(f"发生未知错误:{str(e)}") try: response = requests.get(url, headers=headers1, cookies=cookies, allow_redirects=True, timeout=timeout) name = re.findall(r'<span class="info-username">(.*?)</span>', response.text)[0] print("获取姓名成功") print(name) response.raise_for_status() except requests.exceptions.Timeout: print(f"获取姓名请求超时(超过 {timeout} 秒)") except requests.exceptions.ConnectionError: print("获取姓名连接失败(可能是网络问题或域名错误)") except requests.exceptions.HTTPError as e: print("获取姓名连接失败") except Exception as e: print(f"发生未知错误:{str(e)}")
url = "https://jwgl.squ.edu.cn/student/for-std/course-table" try: response = requests.get(url, headers=headers1, cookies=cookies, allow_redirects=True, timeout=timeout) result = re.findall(r"var semesters = JSON.parse\(\s*'([\s\S]*?)'\s*\)", response.text)[0] std_data = json.loads(result.replace('\\', ''))[0] print("获取学期成功") print(f'当前学期代号{std_data.get("id")}') response.raise_for_status() except requests.exceptions.Timeout: print(f"获取学期请求超时(超过 {timeout} 秒)") except requests.exceptions.ConnectionError: print("获取学期连接失败(可能是网络问题或域名错误)") except requests.exceptions.HTTPError as e: print("获取学期连接失败") except Exception as e: print("获取学期未知错误") try: json_data = json.dumps(std_data, indent=2, ensure_ascii=False) except Exception as e: print(f"发生未知错误:{str(e)}")
try: with open("std_data.json", "w", encoding="utf-8") as f: f.write(json_data) print("学期信息写入成功")
except FileNotFoundError: print("错误:文件路径不存在,无法写入") except PermissionError: print("错误:没有权限写入文件,请检查文件权限") except IOError as e: print(f"写入文件时发生I/O错误:{str(e)}") except Exception as e: print(f"发生未知错误:{str(e)}")
try: url = "https://jwgl.squ.edu.cn/student/for-std/course-table/semester/{}/print-data?semesterId={}&hasExperiment=true".format( std_data.get("id"), std_data.get("id")) except Exception as e: print(f"发生未知错误:{str(e)}") try: response = requests.get(url, headers=headers1, cookies=cookies, allow_redirects=True, timeout=timeout) print("获取课程表成功") response.raise_for_status() except requests.exceptions.Timeout: print(f"获取课程表请求超时(超过 {timeout} 秒)") except requests.exceptions.ConnectionError: print("获取课程表连接失败(可能是网络问题或域名错误)") except requests.exceptions.HTTPError as e: print("获取课程表连接失败") except Exception as e: print(f"获取课程表未知错误:{str(e)}")
try: course = json.loads(response.text) newCourse = json.dumps(course, indent=2, ensure_ascii=False) except Exception as e: print(f"发生未知错误:{str(e)}") try: if not newCourse.strip(): print("newCourse为空,不执行写入操作") else: with open("class.json", "w", encoding="utf-8") as f: f.write(newCourse) print("课程表文件写入成功")
except FileNotFoundError: print("错误:文件路径不存在,无法写入") except PermissionError: print("错误:没有权限写入文件,请检查文件权限") except IOError as e: print(f"写入文件时发生I/O错误:{str(e)}") except Exception as e: print(f"写入文件时发生未知错误:{str(e)}")
def first(timeout): headers = { "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7", "Accept-Encoding": "gzip, deflate", "Accept-Language": "zh-CN,zh;q=0.9", "Cache-Control": "max-age=0", "Connection": "keep-alive", "Host": "authserver.squ.edu.cn", "Upgrade-Insecure-Requests": "1", "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36" } url = "http://authserver.squ.edu.cn/authserver/login" params = { 'service': "https://jwgl.squ.edu.cn/student/sso/login" } try: response = session.get(url, headers=headers, params=params, timeout=timeout) lt = re.findall(r'<input type="hidden" name="lt" value="(.*?)"', response.text)[0] response.raise_for_status() print("lt获取成功") return lt
except requests.exceptions.Timeout: print(f"请求超时(超过 {timeout} 秒)") except requests.exceptions.ConnectionError: print("连接失败(可能是网络问题或域名错误)") except requests.exceptions.HTTPError as e: print("连接失败") except Exception as e: print("未知错误")
if __name__ == '__main__':
try: lt = first(timeout)
login("学号", "密码", lt) except Exception: print("登陆失败")
try: with open("class.json", "r", encoding="utf-8") as file: content = file.read() if not content.strip(): print("文件内容为空不处理") else: print("文件内容不为空,开始处理数据...") classData = json.loads(content) dict_list = []
for activity in classData.get("studentTableVms")[0].get("activities"): newClass = {} newClass["courseName"] = activity.get("courseName") newClass["room"] = activity.get("room") newClass["weekIndexes"] = activity.get("weekIndexes") newClass["weekday"] = activity.get("weekday") newClass["startUnit"] = activity.get("startUnit") newClass["endUnit"] = activity.get("endUnit") dict_list.append(newClass) with open("classNew.json", "w", encoding="utf-8") as f: f.write(json.dumps(dict_list, ensure_ascii=False, indent=2))
except Exception as e:
print(f"覆盖文件失败:{str(e)}")
|