正文
增加 13 行删除 13 行
123 行未改动全部展开1241251261271281291301311321331341351361371381391401411421431441451461471482 行未改动1511521531541551561571581599 行未改动16917017117217317417517617745 行未改动2232242252262272282292302314 行未改动23623723823924024124224324424524624724824925025125225325425525612 行未改动2692702712722732742752762772 行未改动2802812822832842852862872882892902912922932942952962972982993003013023039 行未改动123 行未改动全部展开local_data = []if os.path.exists(FILE_NAME):with open(FILE_NAME, 'r', encoding='utf-8') as f:local_data = json.load(f)latest_id = local_data[0]['id'] if local_data else Nonenew_records = []page = 1while True:print(f"正在抓取第 {page} 页...")try:response = requests.get(f"{URL_BASE}{page}", headers=HEADERS, cookies=COOKIES, timeout=10)res_data = extract_json_from_html(response.text)if not res_data:print("解析 HTML 数据失败,可能是模板已更改或被反爬拦截。")breakrecords = res_data.get("currentData", {}).get("records", {}).get("result", [])if not records:print("抓取完毕或当前页无数据。")break2 行未改动if latest_id and item['id'] == latest_id:print(f"到达本地存量数据位置 (ID: {latest_id}),停止抓取。")stop_found = Truebreak# 按照你的本地 {uid}.json 格式精简字段new_records.append({"time": item.get("time"),"memory": item.get("memory"),9 行未改动})if stop_found:breakpage += 1time.sleep(3)except Exception as e:45 行未改动def get_extension_by_lang(lang_id):"""根据洛谷语言 ID 返回后缀名"""mapping = {0: ".cpp", 1: ".py", 2: ".c", 3: ".cpp", 4: ".cpp",7: ".py", 8: ".py", 11: ".cpp", 12: ".cpp", 13: ".java",14: ".pas", 16: ".php", 20: ".php", 22: ".py", 25: ".py",26: ".cpp", 27: ".cpp", 28: ".cpp", 34: ".cpp"}4 行未改动pattern = re.compile(r'window\._feInjection = JSON\.parse\(decodeURIComponent\("(.+?)"\)\);')match = pattern.search(html_content)if not match:return Falsetry:data = json.loads(unquote(match.group(1)))record = data.get('currentData', {}).get('record', {})source_code = record.get('sourceCode')lang_id = record.get('language')if source_code:ext = get_extension_by_lang(lang_id)pid = record.get('problem', {}).get('pid', 'Unknown')code_filename = f"R{rid}_{pid}{ext}"code_path = CODE_DIR / code_filename # 路径拼接更直观code_path.write_text(source_code, encoding='utf-8')return Trueexcept Exception as e:print(f"\n[Error] 解析 R{rid} 失败: {e}")12 行未改动# 2. 循环爬取详情for i, rid in enumerate(ids):html_path = RECORDS_DIR / f"R{rid}.html"print(f"进度 [{i+1}/{len(ids)}] - 正在处理 R{rid}...", end='\r')# A: 本地缓存处理if html_path.exists():2 行未改动# B: 发起网络请求try:response = requests.get(f"{URL_BASE}{rid}", headers=HEADERS, cookies=COOKIES, timeout=10)if response.status_code == 200:html_text = response.text# 保存 HTML 备份html_path.write_text(html_text, encoding='utf-8')if process_record(rid, html_text):time.sleep(3)else:print(f"\n[Error] R{rid} 无法提取代码")elif response.status_code == 404:print(f"\n[Error] R{rid} 不存在 (404)")else:print(f"\n[Error] R{rid} 状态码: {response.status_code}")time.sleep(5)except Exception as e:print(f"\n[Error] R{rid}: {e}")time.sleep(5)9 行未改动123 行未改动全部展开1241251261271281291301311321331341351361371381391401411421431441451461471482 行未改动1511521531541551561571581599 行未改动16917017117217317417517617745 行未改动2232242252262272282292302314 行未改动23623723823924024124224324424524624724824925025125225325425525612 行未改动2692702712722732742752762772 行未改动2802812822832842852862872882892902912922932942952962972982993003013023039 行未改动123 行未改动全部展开local_data = []if os.path.exists(FILE_NAME):with open(FILE_NAME, 'r', encoding='utf-8') as f:local_data = json.load(f)latest_id = local_data[0]['id'] if local_data else Nonenew_records = []page = 1while True:print(f"正在抓取第 {page} 页...")try:response = requests.get(f"{URL_BASE}{page}", headers=HEADERS, cookies=COOKIES, timeout=10)res_data = extract_json_from_html(response.text)if not res_data:print("解析 HTML 数据失败,可能是模板已更改或被反爬拦截。")breakrecords = res_data.get("currentData", {}).get("records", {}).get("result", [])if not records:print("抓取完毕或当前页无数据。")break2 行未改动if latest_id and item['id'] == latest_id:print(f"到达本地存量数据位置 (ID: {latest_id}),停止抓取。")stop_found = Truebreak# 按照你的本地 {uid}.json 格式精简字段new_records.append({"time": item.get("time"),"memory": item.get("memory"),9 行未改动})if stop_found:breakpage += 1time.sleep(3)except Exception as e:45 行未改动def get_extension_by_lang(lang_id):"""根据洛谷语言 ID 返回后缀名"""mapping = {0: ".cpp", 1: ".py", 2: ".c", 3: ".cpp", 4: ".cpp",7: ".py", 8: ".py", 11: ".cpp", 12: ".cpp", 13: ".java",14: ".pas", 16: ".php", 20: ".php", 22: ".py", 25: ".py",26: ".cpp", 27: ".cpp", 28: ".cpp", 34: ".cpp"}4 行未改动pattern = re.compile(r'window\._feInjection = JSON\.parse\(decodeURIComponent\("(.+?)"\)\);')match = pattern.search(html_content)if not match:return Falsetry:data = json.loads(unquote(match.group(1)))record = data.get('currentData', {}).get('record', {})source_code = record.get('sourceCode')lang_id = record.get('language')if source_code:ext = get_extension_by_lang(lang_id)pid = record.get('problem', {}).get('pid', 'Unknown')code_filename = f"R{rid}_{pid}{ext}"code_path = CODE_DIR / code_filename # 路径拼接更直观code_path.write_text(source_code, encoding='utf-8')return Trueexcept Exception as e:print(f"\n[Error] 解析 R{rid} 失败: {e}")12 行未改动# 2. 循环爬取详情for i, rid in enumerate(ids):html_path = RECORDS_DIR / f"R{rid}.html"print(f"进度 [{i+1}/{len(ids)}] - 正在处理 R{rid}...", end='\r')# A: 本地缓存处理if html_path.exists():2 行未改动# B: 发起网络请求try:response = requests.get(f"{URL_BASE}{rid}", headers=HEADERS, cookies=COOKIES, timeout=10)if response.status_code == 200:html_text = response.text# 保存 HTML 备份html_path.write_text(html_text, encoding='utf-8')if process_record(rid, html_text):time.sleep(3)else:print(f"\n[Error] R{rid} 无法提取代码")elif response.status_code == 404:print(f"\n[Error] R{rid} 不存在 (404)")else:print(f"\n[Error] R{rid} 状态码: {response.status_code}")time.sleep(5)except Exception as e:print(f"\n[Error] R{rid}: {e}")time.sleep(5)9 行未改动