上传文件至 q2_1_crawler
This commit is contained in:
45
q2_1_crawler/q2_2.py
Normal file
45
q2_1_crawler/q2_2.py
Normal file
@@ -0,0 +1,45 @@
|
||||
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}")
|
||||
Reference in New Issue
Block a user