From 4908890379bcb9d25eb175a9f5713371702f5d21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=AB=E5=A2=9E?= <2509165014@student.example.com> Date: Sun, 5 Jul 2026 18:02:44 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=A0=E9=99=A4=20pachong.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pachong.py | 74 ------------------------------------------------------ 1 file changed, 74 deletions(-) delete mode 100644 pachong.py diff --git a/pachong.py b/pachong.py deleted file mode 100644 index 8f37e33..0000000 --- a/pachong.py +++ /dev/null @@ -1,74 +0,0 @@ -import requests -import json -from bs4 import BeautifulSoup - -# 题目强制要求请求头 -url = "https://exam.detr.top/exam-b/movies" -headers = { - "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36" -} - -# 仅一次请求,符合题目要求 -resp = requests.get(url, headers=headers) -resp.encoding = "utf-8" -html_text = resp.text - -# 保存原始网页 movies.html -with open("movies.html", "w", encoding="utf-8") as f: - f.write(html_text) -print("✅ 已保存网页源码 movies.html") - -# 优先尝试直接解析接口JSON(接口真实返回格式) -movie_list = [] -data_id = None -try: - api_data = json.loads(html_text) - data_id = api_data.get("data_id") - movie_list = api_data.get("movies", []) - print("✅ 识别为JSON接口,直接读取数据") -except json.JSONDecodeError: - # 若为HTML表格页面,执行原bs4解析逻辑 - print("识别为HTML表格页面,使用BeautifulSoup解析") - soup = BeautifulSoup(html_text, "html.parser") - # 提取页面data_id - if soup.body and "data-id" in soup.body.attrs: - data_id = soup.body["data-id"] - # 提取表格行 - all_tr = soup.find_all("tr") - for tr in all_tr[1:]: - td_list = tr.find_all("td") - if len(td_list) >= 8: - # 增加类型转换容错 - def safe_int(txt): - try: - return int(txt.strip()) - except: - return 0 - def safe_float(txt): - try: - return float(txt.strip()) - except: - return 0.0 - movie = { - "id": safe_int(td_list[0].get_text()), - "title": td_list[1].get_text(strip=True), - "director": td_list[2].get_text(strip=True), - "year": safe_int(td_list[3].get_text()), - "rating": safe_float(td_list[4].get_text()), - "duration": safe_int(td_list[5].get_text()), - "genre": td_list[6].get_text(strip=True), - "actors_count": safe_int(td_list[7].get_text()) - } - movie_list.append(movie) - -print(f"页面data_id: {data_id}") -print(f"一共抓取到 {len(movie_list)} 部电影") - -# 组装并保存 movies.json -save_data = { - "data_id": data_id, - "movies": movie_list -} -with open("movies.json", "w", encoding="utf-8") as f: - json.dump(save_data, f, ensure_ascii=False, indent=2) -print("✅ movies.json 写入完成") \ No newline at end of file