We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
get_block_courses 函数中:
get_block_courses
for i in range(len(temp_list)): temp_list[i].update(block_list[i]) result = { "count": len(temp_list), "courses": [ { "course_id": j["kch_id"], "class_id": j.get("jxb_id"), "do_id": j.get("do_jxb_id"), "title": j.get("kcmc"), "teacher_id": (re.findall(r"(.*?\d+)/", j.get("jsxx")))[0], "teacher": (re.findall(r"/(.*?)/", j.get("jsxx")))[0], "credit": float(j.get("xf"), 0), "kklxdm": head_data[f"bkk{block}_kklxdm"], "capacity": int(i.get("jxbrl", 0)), "selected_number": int(i.get("yxzrs", 0)), "place": self.get_place(i.get("jxdd")), "time": self.get_course_time(i.get("sksj")), } for j in temp_list ], }
为何在后面的几个索引使用的是i.而不是j.?
i.
j.
我实现了一个简单的自动抢课脚本:
import os import sys import time import base64 from pprint import pprint from concurrent.futures import ThreadPoolExecutor, as_completed from typing import Dict, Any from zfn_api import Client def fetch_all_courses(client: Client, year: int, term: int) -> Dict[str, Any]: """ 使用框架方法 get_block_courses,遍历 block=1,2,3, 收集所有可选课程并合并返回。 若某个 block 返回 code!=1000,则直接中断并返回对应结构。 """ all_courses = [] for block in [1, 2, 3]: print(f"\n>>> 正在获取第 {block} 个板块的课程...") block_res = client.get_block_courses(year, term, block) # 如果出现非选课阶段、或其它错误,直接返回 if block_res["code"] != 1000: print("获取板块课失败:", block_res) return block_res courses_data = block_res["data"]["courses"] print(f"第 {block} 个板块获取到课程数量:", len(courses_data)) all_courses.extend(courses_data) return { "code": 1000, "msg": "获取全部板块课程成功", "data": {"courses": all_courses}, } def main(): # 这里请根据实际教务系统情况填写 base_url = "https://jwxt.njpji.cn/jwglxt/" # 注意,与框架内 URL 拼接保持一致 # 1) 输入学号、密码、关键字 sid = input("请输入学号: ").strip() pwd = input("请输入密码: ").strip() keyword = input("请输入想要搜索的课程关键字: ").strip() # 2) 初始化客户端 stu = Client(base_url=base_url, timeout=5) # 3) 登录 pre_login = stu.login(sid, pwd) if pre_login["code"] == 1001: # 如果需要验证码 verify_data = pre_login["data"] with open(os.path.abspath("kaptcha.png"), "wb") as pic: pic.write(base64.b64decode(verify_data.pop("kaptcha_pic"))) print("验证码图片已保存到 kaptcha.png,请查看后输入验证码。") verify_data["kaptcha"] = input("请输入验证码: ").strip() ret = stu.login_with_kaptcha(**verify_data) if ret["code"] != 1000: print("登录失败:", ret) sys.exit(1) else: print("验证码登录成功:", ret) elif pre_login["code"] == 1000: print("登录成功:", pre_login) else: # 登录出错或用户名/密码错误 print("登录失败:", pre_login) sys.exit(1) # 设置选课参数 year, term = 2024, 2 # 示例学年、学期 max_workers = 5 # 并发线程数 retry_interval = 1 # 重试间隔(秒),根据需求调整 max_retries = 1000 # 最大重试次数,可根据需要调整 try: # 开始循环选课 for attempt in range(1, max_retries + 1): print(f"\n==== [第 {attempt} 次尝试获取所有板块课程] ====") all_res = fetch_all_courses(stu, year, term) # 如果非选课阶段、或出现错误,等待后重试 if all_res["code"] != 1000: pprint(all_res) print(f"当前状态:{all_res['msg']},将在 {retry_interval} 秒后重试。") time.sleep(retry_interval) continue all_courses = all_res["data"]["courses"] print(f"\n=== 成功获取到全部板块课程,共 {len(all_courses)} 门 ===") # 在所有课程中,筛选名称包含关键字的课程 matched = [c for c in all_courses if keyword in c["title"]] if not matched: print(f"未找到包含关键词【{keyword}】的课程,等待 {retry_interval} 秒后重试。") time.sleep(retry_interval) continue print(f"\n=== 找到 {len(matched)} 门包含“{keyword}”的课程,准备选课 ===") # 多线程并发选课 with ThreadPoolExecutor(max_workers=max_workers) as executor: future_to_course = { executor.submit( stu.select_course, sid, # 学号 c["course_id"],# 课程ID c["do_id"], # do_id c["kklxdm"], # 课程类型代码 year, term ): c for c in matched } # 等待所有线程完成 for future in as_completed(future_to_course): course_info = future_to_course[future] try: res = future.result() # 选课结果 print(f"[选课结果] {course_info['title']} -> ", res) if res["code"] == 1000: # 选课成功,输出成功信息并立即退出 print(f"\n=== 成功选课:{course_info['title']} ===") sys.exit(0) except Exception as exc: print(f"[异常] {course_info['title']} 选课出现异常: {exc}") print(f"\n=== 尝试未成功选取任何课程,等待 {retry_interval} 秒后重试 ===") time.sleep(retry_interval) except KeyboardInterrupt: print("\n程序被用户中断,退出。") else: print("\n=== 达到最大重试次数,选课结束。尚有课程未成功选课 ===") print("程序结束。") if __name__ == "__main__": main()
因为学校不是持续开放选课,我不能确定接口的具体用法,很抱歉给您添麻烦了。
The text was updated successfully, but these errors were encountered:
No branches or pull requests
代码实现疑问:
get_block_courses
函数中:为何在后面的几个索引使用的是
i.
而不是j.
?是否可以提供一下具体的示例?
我实现了一个简单的自动抢课脚本:
因为学校不是持续开放选课,我不能确定接口的具体用法,很抱歉给您添麻烦了。
The text was updated successfully, but these errors were encountered: