只针对 https://ouc-sz.chinahrt.com/center/MyExam
import requests
import time
import random
import pandas as pd
from openpyxl import load_workbookexcel_file = r'C:\Users\N3verL4nd\Desktop\exam_answers.xlsx'# 定义请求头,包含 Authorization
headers = {"Authorization": "你的token","Referer": "https://ouc-sz.chinahrt.com/exam?examInfo=%7B%22classBatchId%22%3A%22389140424587673600%22,%22examId%22%3A%223f91a58085d5408a8cb3364ce65db556%22%7D","sec-ch-ua": '"Google Chrome";v="131", "Chromium";v="131", "Not_A Brand";v="24"',"sec-ch-ua-mobile": "?0","sec-ch-ua-platform": "Windows","user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36"
}for i in range(10):sleep_time = random.randint(0, 10)print(f"第 {i + 1} 次休眠,时间为: {sleep_time} 秒")answer_list = []# 第一步:发送 GET 请求get_url = "https://ouc-sz.chinahrt.com/api/web/exam/goExam?examId=3f91a58085d5408a8cb3364ce65db556"get_response = requests.get(get_url, headers=headers)# 检查请求是否成功if get_response.status_code == 200:# 解析 JSON 响应response_data = get_response.json()if response_data.get("code") == "SUCCESS":# 获取 idrecord_id = response_data["data"]["recordInfo"]["id"]print(f"Retrieved recordId: {record_id}")# 第二步:发送 POST 请求post_url = "https://ouc-sz.chinahrt.com/api/web/exam/submit"post_data = {'recordId': record_id}# 发送 POST 请求post_response = requests.post(post_url, headers=headers, data=post_data)if post_response.status_code == 200:print("POST request successful")print(post_response.json()) # 打印返回的 JSON 数据# 获取当前已有的 question_id 列表(避免重复保存)existing_question_ids = set()try:# 如果 Excel 文件已经存在,加载文件并读取 question_id 列df_existing = pd.read_excel(excel_file, sheet_name='Sheet1')existing_question_ids = set(df_existing['question_id'].values)except FileNotFoundError:# 如果文件不存在,则跳过print(f"File {excel_file} not found. A new file will be created.")# 遍历返回的答题列表for item in post_response.json()['data']['answerList']:submit_id = item['id']question_id = item['questionData']['questionBasicInfo']['id']content = item['questionData']['questionBasicInfo']['content']answer = item['questionData']['questionBasicInfo']['answer']# 只有当 question_id 不在现有文件中时,才添加到 answer_listif question_id not in existing_question_ids:answer_list.append({'submit_id': submit_id, 'question_id': question_id, 'Content': content, 'Answer': answer})existing_question_ids.add(question_id) # 更新已保存的 question_idelse:print(f"POST request failed with status code {post_response.status_code}")else:print("Error: Invalid response code.")else:print(f"GET request failed with status code {get_response.status_code}")# 如果有新的数据,保存到 Excel 文件if answer_list:df = pd.DataFrame(answer_list)# 如果文件存在,追加数据;如果文件不存在,则创建新文件try:with pd.ExcelWriter(excel_file, mode='a', engine='openpyxl', if_sheet_exists='overlay') as writer:writer.workbook = load_workbook(excel_file)if 'Sheet1' in writer.book.sheetnames:df.to_excel(writer, index=False, header=False, sheet_name='Sheet1', startrow=writer.book['Sheet1'].max_row)else:df.to_excel(writer, index=False, sheet_name='Sheet1')print(f"数据已追加到 {excel_file}")except FileNotFoundError:# 如果文件不存在,创建新的 Excel 文件并保存数据df.to_excel(excel_file, index=False, engine='openpyxl')print(f"文件不存在,已创建新文件并保存数据到 {excel_file}")else:print("没有新的答案需要保存。")time.sleep(sleep_time)