From e5cfeea035ebb75780f023560e97f53d36ba95b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BD=97=E6=AC=A3=E6=80=A1?= <2509165012@student.example.com> Date: Tue, 23 Jun 2026 11:02:44 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E6=96=87=E4=BB=B6=E8=87=B3?= =?UTF-8?q?=20/?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 2.py | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 3.py | Bin 0 -> 1024 bytes text.py | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 101 insertions(+) create mode 100644 2.py create mode 100644 3.py create mode 100644 text.py diff --git a/2.py b/2.py new file mode 100644 index 0000000..28b13c2 --- /dev/null +++ b/2.py @@ -0,0 +1,46 @@ +import json +from collections import defaultdict + +# 读取数据 +with open("movies.json", "r", encoding="utf-8") as f: + data = json.load(f) +movies = data.get("movies", []) + +# ① 找出评分最高和最低的电影 +if movies: + # 按rating排序,处理可能的空值 + sorted_by_rating = sorted(movies, key=lambda x: float(x["rating"]) if x["rating"] else 0) + lowest = sorted_by_rating[0] + highest = sorted_by_rating[-1] + print("评分最低:", lowest["title"], lowest["rating"]) + print("评分最高:", highest["title"], highest["rating"]) +else: + print("无电影数据") + +# ② 统计各类型的电影数量(注意genre可能是列表或字符串,这里按常见的逗号分隔处理) +genre_count = defaultdict(int) +for movie in movies: + genres = movie["genre"] + # 如果genre是字符串,按逗号分割;如果是列表,直接遍历 + if isinstance(genres, str): + genres = [g.strip() for g in genres.split(",")] + for g in genres: + genre_count[g] += 1 +print("\n各类型电影数量:", dict(genre_count)) + +# ③ 统计各导演的电影数量 +director_count = defaultdict(int) +for movie in movies: + director = movie["director"] + director_count[director] += 1 +print("\n各导演电影数量:", dict(director_count)) + +# ④ 统计2020年(含)以后上映的电影数量 +count_2020 = 0 +for movie in movies: + year = movie["year"] + if isinstance(year, (int, float)) and year >= 2020: + count_2020 += 1 + elif isinstance(year, str) and year.isdigit() and int(year) >= 2020: + count_2020 += 1 +print(f"\n2020年(含)以后上映的电影数量:{count_2020}") \ No newline at end of file diff --git a/3.py b/3.py new file mode 100644 index 0000000000000000000000000000000000000000..06d7405020018ddf3cacee90fd4af10487da3d20 GIT binary patch literal 1024 ScmZQz7zLvtFd70QH3R?z00031 literal 0 HcmV?d00001 diff --git a/text.py b/text.py new file mode 100644 index 0000000..b849d00 --- /dev/null +++ b/text.py @@ -0,0 +1,55 @@ +import requests +import json + +# 目标网址 +url = "https://exam.detr.top/exam-b/movies" + +# 发送请求获取网页内容 +response = requests.get(url) +response.encoding = "utf-8" # 避免中文乱码 + +# 1. 保存原始网页源码为 movies.html +with open("movies.html", "w", encoding="utf-8") as f: + f.write(response.text) + +# 假设网页返回的是包含数据编号和电影列表的JSON(若实际结构不同,可根据网页返回调整) +# 先解析响应内容(如果网页是HTML+JS渲染,可能需要用BeautifulSoup提取数据) +# 这里先按题目要求,提取数据编号和10部电影信息 +# 注意:如果网页是静态HTML,需用BeautifulSoup解析;如果是直接返回JSON,直接json.loads即可 + +# 方式1:如果网页直接返回JSON(示例,需根据实际网页结构调整) +try: + data = json.loads(response.text) +except json.JSONDecodeError: + # 方式2:如果是HTML,用BeautifulSoup提取数据(这里给通用模板) + from bs4 import BeautifulSoup + soup = BeautifulSoup(response.text, "html.parser") + # 假设数据在script标签中,或直接在HTML表格/列表中,需根据实际结构提取 + # 这里为了适配题目,先模拟数据结构(实际使用时替换为真实提取逻辑) + data = { + "data_id": "demo_id", # 数据编号,根据网页实际获取 + "movies": [] # 10部电影列表,每部包含题目要求的键 + } + +# 提取数据编号和电影信息(按题目要求的键) +result = { + "data_id": data.get("data_id", ""), + "movies": [] +} +for movie in data.get("movies", []): + # 按题目要求保留指定键 + filtered_movie = { + "id": movie.get("id"), + "title": movie.get("title"), + "director": movie.get("director"), + "year": movie.get("year"), + "rating": movie.get("rating"), + "duration": movie.get("duration"), + "genre": movie.get("genre"), + "actors_count": movie.get("actors_count") + } + result["movies"].append(filtered_movie) + +# 保存为 movies.json +with open("movies.json", "w", encoding="utf-8") as f: + json.dump(result, f, ensure_ascii=False, indent=4) \ No newline at end of file