Files

45 lines
1.4 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import json
# 读取爬取结果
with open("movies.json", "r", encoding="utf-8") as f:
data = json.load(f)
movies = data["movies"]
# 容错判断,避免无数据时报错
if not movies:
print("错误无电影数据请先运行爬虫q2_1.py")
else:
# ① 最高、最低评分电影
sorted_movies = sorted(movies, key=lambda x: x["rating"])
min_m = sorted_movies[0]
max_m = sorted_movies[-1]
print("① 评分极值:")
print(f"最低分:{min_m['title']} {min_m['rating']}")
print(f"最高分:{max_m['title']} {max_m['rating']}")
# ② 各类型数量,字典输出
genre_stat = {}
for m in movies:
g = m["genre"]
genre_stat[g] = genre_stat.get(g, 0) + 1
print("\n② 电影类型统计字典:")
print(genre_stat)
# ③ 各导演影片数量,字典输出
dir_stat = {}
for m in movies:
d = m["director"]
dir_stat[d] = dir_stat.get(d, 0) + 1
print("\n③ 导演影片数量统计字典:")
print(dir_stat)
# ④ 2020年后上映总数
count_2020 = 0
for m in movies:
try:
year = int(m["year"])
if year >= 2020:
count_2020 += 1
except ValueError:
continue
print(f"\n④ 2020年后上映电影总数{count_2020}")