From 0bae8e00604e4f7ff5f80ba2e27acc4cc89c89d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BD=97=E8=89=BA=E6=AC=A3?= <2509165020@student.example.com> Date: Tue, 23 Jun 2026 10:25:13 +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 --- movies.html | 152 ++++++++++++++++++++++++++++++++++++++++++++++++++++ movies.json | 105 ++++++++++++++++++++++++++++++++++++ q2_1.py | 34 ++++++++++++ q2_2.py | 43 +++++++++++++++ 4 files changed, 334 insertions(+) create mode 100644 movies.html create mode 100644 movies.json create mode 100644 q2_1.py create mode 100644 q2_2.py diff --git a/movies.html b/movies.html new file mode 100644 index 0000000..d7d3d2d --- /dev/null +++ b/movies.html @@ -0,0 +1,152 @@ + + + + + + + + 电影列表 + + + +

电影列表

+

数据编号:B-20260623-9063

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
编号电影名导演上映年份评分时长(分钟)类型主演数
1霸王别姬Frank Darabont20216.1120冒险2
2放牛班的春天陈凯歌20127.2131科幻4
3星际穿越Robert Zemeckis19928.393冒险2
4盗梦空间James Cameron19988.9127剧情2
5千与千寻宫崎骏20127.2113动画4
6阿甘正传Christopher Nolan19917.8140喜剧5
7肖申克的救赎Lasse Hallström20157.8111科幻2
8忠犬八公的故事Rajkumar Hirani20149.3105喜剧3
9泰坦尼克号Christophe Barratier20229.4130剧情3
10三傻大闹宝莱坞Christopher Nolan20217.6145科幻4
+ + \ No newline at end of file diff --git a/movies.json b/movies.json new file mode 100644 index 0000000..467b041 --- /dev/null +++ b/movies.json @@ -0,0 +1,105 @@ +{ + "data_id": 1, + "movies": [ + { + "id": 1, + "title": "霸王别姬", + "director": "Frank Darabont", + "year": 2021, + "rating": 6.1, + "duration": 120, + "genre": "冒险", + "actors_count": 2 + }, + { + "id": 2, + "title": "放牛班的春天", + "director": "陈凯歌", + "year": 2012, + "rating": 7.2, + "duration": 131, + "genre": "科幻", + "actors_count": 4 + }, + { + "id": 3, + "title": "星际穿越", + "director": "Robert Zemeckis", + "year": 1992, + "rating": 8.3, + "duration": 93, + "genre": "冒险", + "actors_count": 2 + }, + { + "id": 4, + "title": "盗梦空间", + "director": "James Cameron", + "year": 1998, + "rating": 8.9, + "duration": 127, + "genre": "剧情", + "actors_count": 2 + }, + { + "id": 5, + "title": "千与千寻", + "director": "宫崎骏", + "year": 2012, + "rating": 7.2, + "duration": 113, + "genre": "动画", + "actors_count": 4 + }, + { + "id": 6, + "title": "阿甘正传", + "director": "Christopher Nolan", + "year": 1991, + "rating": 7.8, + "duration": 140, + "genre": "喜剧", + "actors_count": 5 + }, + { + "id": 7, + "title": "肖申克的救赎", + "director": "Lasse Hallström", + "year": 2015, + "rating": 7.8, + "duration": 111, + "genre": "科幻", + "actors_count": 2 + }, + { + "id": 8, + "title": "忠犬八公的故事", + "director": "Rajkumar Hirani", + "year": 2014, + "rating": 9.3, + "duration": 105, + "genre": "喜剧", + "actors_count": 3 + }, + { + "id": 9, + "title": "泰坦尼克号", + "director": "Christophe Barratier", + "year": 2022, + "rating": 9.4, + "duration": 130, + "genre": "剧情", + "actors_count": 3 + }, + { + "id": 10, + "title": "三傻大闹宝莱坞", + "director": "Christopher Nolan", + "year": 2021, + "rating": 7.6, + "duration": 145, + "genre": "科幻", + "actors_count": 4 + } + ] +} \ No newline at end of file diff --git a/q2_1.py b/q2_1.py new file mode 100644 index 0000000..b0369c3 --- /dev/null +++ b/q2_1.py @@ -0,0 +1,34 @@ +import requests +from bs4 import BeautifulSoup as bs +import json +headers = { + "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64)" + "AppleWebKit/537.36 (KHTML, like Gecko)" + "Chrome/129.0.0.0 Safari/537.36" +} +url = "https://exam.detr.top/exam-b/movies" +data = [] +resp = requests.get(url, headers=headers) +resp.encoding = 'utf-8' +html_content = resp.text +with open("movies.html", "w", encoding="utf-8") as f: + f.write(html_content) +soup = bs(html_content, "html.parser") +movies = [] +table = soup.find("table") +rows = table.find_all("tr")[1:] +for row in rows: + cols = row.find_all("td") + movie = { + "id": int(cols[0].text.strip()), + "title": cols[1].text.strip(), + "director": cols[2].text.strip(), + "year": int(cols[3].text.strip()), + "rating": float(cols[4].text.strip()), + "duration": int(cols[5].text.strip()), + "genre": cols[6].text.strip(), + "actors_count": int(cols[7].text.strip()) + } + movies.append(movie) +with open("movies.json", "w", encoding="utf-8") as f: + json.dump({"data_id": 1, "movies": movies}, f, ensure_ascii=False, indent=4) diff --git a/q2_2.py b/q2_2.py new file mode 100644 index 0000000..3255ca0 --- /dev/null +++ b/q2_2.py @@ -0,0 +1,43 @@ +import json + +# 读取本地json文件 +with open("movies.json", "r", encoding="utf-8") as f: + data = json.load(f) +movie_list = data["movies"] + +# ① 找出评分最高、最低电影并打印 +ratings = [m["rating"] for m in movie_list] +max_rating = max(ratings) +min_rating = min(ratings) + +max_movies = [m for m in movie_list if m["rating"] == max_rating] +min_movies = [m for m in movie_list if m["rating"] == min_rating] + +print("===== 1. 评分极值电影 =====") +print("评分最高的电影:") +for m in max_movies: + print(f"{m['title']} - {m['rating']}") +print("评分最低的电影:") +for m in min_movies: + print(f"{m['title']} - {m['rating']}") + +# ② 统计各类型电影数量 +genre_count = {} +for m in movie_list: + g = m["genre"] + genre_count[g] = genre_count.get(g, 0) + 1 +print("\n===== 2. 各类型电影数量 =====") +print(genre_count) + +# ③ 统计各导演电影数量 +director_count = {} +for m in movie_list: + d = m["director"] + director_count[d] = director_count.get(d, 0) + 1 +print("\n===== 3. 各导演电影数量 =====") +print(director_count) + +# ④ 统计2020(含)年后上映电影数量 +count_after_2020 = sum(1 for m in movie_list if m["year"] >= 2020) +print("\n===== 4. 2020年(含)后上映电影总数 =====") +print(count_after_2020) \ No newline at end of file