import requests import time import json from datetime import datetime, timedelta # ==================== 配置区(必须修改) ==================== # 1. 从浏览器复制完整的 Cookie 字符串(整个 Cookie 头,例如 "JSESSIONID=xxx; ...") COOKIE_STRING = "你的Cookie字符串" # 请替换 # 2. 固定不变的参数(从你提供的 URL 中提取,已包含 commonQuery) BASE_PARAMS = { "menuName": "组织维护", "useSealTreeData": "0", "treePermitType": "permit", "cancelNodeClick": "1", "clickFlag": "true", "in_use": "1", "commonQuery": "BHasdJ8BHQ8BH2gPLJrPkrzNKJWn8IgdSIhEGIgdSIjd0!3Lj5gnj*YoU*3NK5Ycqr_LiJHeGIjd8IgdSIgEgIgdUfrnke8PU*TNjJ3BH2gBHD!BH2gC8IgdSIgEgIgdk*inLBvJ0ranGIgdSIhEGIgdUrYBH2gBHB9BH2gPLJrPkrKmKfinGIgdSIhEGIgdT(rdT2rdqdrdTBfQKJgoJero0ErdT2rd3(rdT2fBH2gBHB9BH2gPUrkN0e8PU*TNjJ3BH2gBHD!BH2gCGIgdSIgEgIgdUfZnjrTBH2gBHD!BH2gEIv(BH2gBHB9BH2gNKvaQLeIoL!rBH2gBHD!BH2gPjJWnKD3BH2gBHQ(BHJ(yyryrtweasd", "newCondition": "", "urlConditions": "_t%3A1788589944798%23clickFlag%3Atrue%23cancelNodeClick%3A1%23treePermitType%3Apermit%23useSealTreeData%3A0%23menuName%3A%E7%BB%84%E7%BB%87%E7%BB%B4%E6%8A%A4%23_t%3A1788589944798%23", "exportConfig": "", "tableId": "assemble-pt_organzation", "rosterId": "110118b4-b094-11ea-b588-14187743d052", "includeChildrenSetting": "", "struTreeCode": "cq37du", "djStruTreeCode": "", "djStruId": "", "djOrgName": "", "struId": "596bae33-50e9-11ec-b526-822ff8ed5b28", "belongUnitStruId": "596bae33-50e9-11ec-b526-822ff8ed5b28", "rootId": "cq37du", "isVirtual": "0", "currentUserRootId": "cq37du", "currentUserId": "178b0efa-8a65-11f1-a7d9-000c296f9469", "currentRoleIds": "POST_SYS_ADMIN,renshi", "currentStaffId": "1342173e-36cf-4160-8826-6b17d7d084a4", "baseHrefPath": "http://hr.37du.co:80RedseaPlatform", "qrCodeUrl": "jspresumepage-01.jsp", "orgName": "集团", "belongUnitOrgName": "集团", "throughParam": "", "sortType": "", "sortName": "", } URL = "https://hr.37du.co/RedseaPlatform/hrRoster/110118b4-b094-11ea-b588-14187743d052/queryRealTimeData.mc" PAGE_SIZE = 500 # 最大支持 500 # ==================== 动态日期生成 ==================== def get_date_params(): now = datetime.now() year, month, day = now.year, now.month, now.day current_date = f"{year}-{month:02d}-{day:02d}" current_month = f"{year}{month:02d}" current_month_bar = f"{year}-{month:02d}" # 上月 first_of_this_month = datetime(year, month, 1) last_month_date = first_of_this_month - timedelta(days=1) last_month_year, last_month_month = last_month_date.year, last_month_date.month last_month_str = f"{last_month_year}{last_month_month:02d}" last_mon_first_day = f"{last_month_year}-{last_month_month:02d}-01" last_mon_last_day = last_month_date.strftime("%Y-%m-%d") cur_year_first_mon = f"{year}01" cur_year_first_mon_bar = f"{year}-01" return { "currentDate": current_date, "currentMonth": current_month, "currentMonthBar": current_month_bar, "lastMonth": last_month_str, "lastMonFirstDay": last_mon_first_day, "lastMonLastDay": last_mon_last_day, "curYearFirstMon": cur_year_first_mon, "curYearFirstMonBar": cur_year_first_mon_bar, } # ==================== 请求函数 ==================== def fetch_page(page_no, start, date_params): params = BASE_PARAMS.copy() params.update(date_params) params["pageNo"] = str(page_no) params["pageSize"] = str(PAGE_SIZE) params["start"] = str(start) params["_t"] = str(int(time.time() * 1000)) cookies = {} for item in COOKIE_STRING.split(';'): item = item.strip() if '=' in item: key, value = item.split('=', 1) cookies[key] = value resp = requests.post(URL, params=params, cookies=cookies, timeout=30) resp.raise_for_status() return resp.json() # ==================== 主循环 ==================== def main(): if COOKIE_STRING == "你的Cookie字符串": print("⚠️ 请先设置有效的 COOKIE_STRING!") return page_no = 1 start = 0 all_data = [] total_count = None page_count = None print(f"开始拉取数据,每页 {PAGE_SIZE} 条...") while True: print(f"正在请求第 {page_no} 页 (start={start}) ...") date_params = get_date_params() try: resp_json = fetch_page(page_no, start, date_params) except Exception as e: print(f"❌ 请求失败: {e}") break # 检查返回状态 if resp_json.get("state") != "1": print(f"❌ 接口返回错误: {resp_json.get('meg') or resp_json.get('des')}") break # 获取数据 datas = resp_json.get("datas", []) if not datas: print("✅ 本页无数据,结束拉取。") break all_data.extend(datas) print(f" 本页获取 {len(datas)} 条,累计 {len(all_data)} 条") # 获取总页数和总条数(第一次请求时保存) if page_no == 1: total_count = resp_json.get("count") page_count = resp_json.get("pageCount") if total_count is not None: print(f" 总记录数: {total_count},总页数: {page_count}") # 判断是否最后一页 if page_count is not None and page_no >= page_count: print(f"✅ 已拉取全部 {total_count} 条数据。") break # 如果返回的数据少于 PAGE_SIZE,也视为结束(兜底) if len(datas) < PAGE_SIZE: print("✅ 本页数据不足一页,视为最后一页。") break # 继续下一页 page_no += 1 start += PAGE_SIZE print(f"\n🎉 拉取完成,共获取 {len(all_data)} 条记录。") # 保存为 JSON 文件 with open("org_data.json", "w", encoding="utf-8") as f: json.dump(all_data, f, ensure_ascii=False, indent=2) print("数据已保存到 org_data.json") if __name__ == "__main__": main()