上传文件至 /

This commit is contained in:
2026-06-23 11:02:44 +08:00
parent 2507150226
commit e5cfeea035
3 changed files with 101 additions and 0 deletions

46
2.py Normal file
View File

@@ -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}")

BIN
3.py Normal file

Binary file not shown.

55
text.py Normal file
View File

@@ -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)