From 67d8e5528bcfffc9473a85e566aefdceb1bdbd00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=B1=BF=E8=B1=AA?= <2509165005@student.example.com> Date: Sun, 5 Jul 2026 21:56:18 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E8=87=B3=20q2=5F1=5Fcrawler?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- q2_1_crawler/movie.py | 87 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 q2_1_crawler/movie.py diff --git a/q2_1_crawler/movie.py b/q2_1_crawler/movie.py new file mode 100644 index 0000000..e887138 --- /dev/null +++ b/q2_1_crawler/movie.py @@ -0,0 +1,87 @@ +import requests +import json +from bs4 import BeautifulSoup + +url = "https://exam.detr.top/exam-b/movies" +headers = { + "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36" +} + +resp = requests.get(url, headers=headers) +resp.encoding = resp.apparent_encoding +html_source = resp.text + +with open("movies.html", "w", encoding="utf-8") as f: + f.write(html_source) +print("已保存原始网页:movies.html") + +soup = BeautifulSoup(html_source, "html.parser") + +code_text = soup.find("code").get_text(strip=True) +data_id = code_text.split(":")[-1].strip() + +table_rows = soup.find("table").find_all("tr")[1:] +movie_list = [] + +for row in table_rows: + tds = row.find_all("td") + movie_info = { + "id": int(tds[0].text), + "title": tds[1].text, + "director": tds[2].text, + "year": int(tds[3].text), + "rating": float(tds[4].text), + "duration": int(tds[5].text), + "genre": tds[6].text, + "actors_count": int(tds[7].text) + } + movie_list.append(movie_info) + +# 组装json总数据 +json_data = { + "data_code": data_id, + "movies": movie_list +} + +# 保存movies.json +with open("movies.json", "w", encoding="utf-8") as f: + json.dump(json_data, f, ensure_ascii=False, indent=4) +print("已保存电影数据:movies.json") + +# ====================== 第2题:读取movies.json进行数据分析 ====================== +# 读取json文件 +with open("movies.json", "r", encoding="utf-8") as f: + load_data = json.load(f) +movies = load_data["movies"] + +# ① 找出评分最高、最低电影 +sorted_by_rating = sorted(movies, key=lambda x: x["rating"]) +min_movie = sorted_by_rating[0] +max_movie = sorted_by_rating[-1] +print("\n===== ① 评分最高/最低电影 =====") +print(f"评分最低:{min_movie['title']} {min_movie['rating']}") +print(f"评分最高:{max_movie['title']} {max_movie['rating']}") + +# ② 统计各类型电影数量 +genre_count = {} +for m in movies: + g = m["genre"] + genre_count[g] = genre_count.get(g, 0) + 1 +print("\n===== ② 各类型电影数量 =====") +print(genre_count) + +# ③ 统计各导演电影数量 +director_count = {} +for m in movies: + d = m["director"] + director_count[d] = director_count.get(d, 0) + 1 +print("\n===== ③ 各导演电影数量 =====") +print(director_count) + +# ④ 统计2020年(含)以后上映电影数量 +cnt_after_2020 = 0 +for m in movies: + if m["year"] >= 2020: + cnt_after_2020 += 1 +print("\n===== ④ 2020年(含)后上映电影总数 =====") +print(cnt_after_2020) \ No newline at end of file From 1c5f2956e671847a2c84fe838b2bf0fc3191eedb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=B1=BF=E8=B1=AA?= <2509165005@student.example.com> Date: Sun, 5 Jul 2026 21:56:33 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E8=87=B3=20q2=5F1=5Fcrawler?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- q2_1_crawler/movies.html | 152 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 152 insertions(+) create mode 100644 q2_1_crawler/movies.html diff --git a/q2_1_crawler/movies.html b/q2_1_crawler/movies.html new file mode 100644 index 0000000..f7839ce --- /dev/null +++ b/q2_1_crawler/movies.html @@ -0,0 +1,152 @@ + + + + + + + + 电影列表 + + + +

电影列表

+

数据编号:B-20260705-9162

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
编号电影名导演上映年份评分时长(分钟)类型主演数
1放牛班的春天Frank Darabont20078.9155悬疑5
2霸王别姬陈凯歌20186.6143剧情4
3星际穿越Robert Zemeckis20188.4165悬疑5
4肖申克的救赎James Cameron20088.6124剧情5
5盗梦空间宫崎骏19937.890爱情2
6泰坦尼克号Christopher Nolan20016.7175喜剧4
7忠犬八公的故事Lasse Hallström20048.291动画3
8三傻大闹宝莱坞Rajkumar Hirani20116.3175冒险3
9阿甘正传Christophe Barratier20227.9107动画4
10千与千寻Christopher Nolan19937.2129悬疑3
+ + \ No newline at end of file From a48acdad2552a0dd4f3db2345ad8092003820c28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=B1=BF=E8=B1=AA?= <2509165005@student.example.com> Date: Sun, 5 Jul 2026 21:56:51 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E8=87=B3=20q2=5F1=5Fcrawler?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- q2_1_crawler/movies.json | 105 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 q2_1_crawler/movies.json diff --git a/q2_1_crawler/movies.json b/q2_1_crawler/movies.json new file mode 100644 index 0000000..7380a6e --- /dev/null +++ b/q2_1_crawler/movies.json @@ -0,0 +1,105 @@ +{ + "data_code": "B-20260705-9162", + "movies": [ + { + "id": 1, + "title": "放牛班的春天", + "director": "Frank Darabont", + "year": 2007, + "rating": 8.9, + "duration": 155, + "genre": "悬疑", + "actors_count": 5 + }, + { + "id": 2, + "title": "霸王别姬", + "director": "陈凯歌", + "year": 2018, + "rating": 6.6, + "duration": 143, + "genre": "剧情", + "actors_count": 4 + }, + { + "id": 3, + "title": "星际穿越", + "director": "Robert Zemeckis", + "year": 2018, + "rating": 8.4, + "duration": 165, + "genre": "悬疑", + "actors_count": 5 + }, + { + "id": 4, + "title": "肖申克的救赎", + "director": "James Cameron", + "year": 2008, + "rating": 8.6, + "duration": 124, + "genre": "剧情", + "actors_count": 5 + }, + { + "id": 5, + "title": "盗梦空间", + "director": "宫崎骏", + "year": 1993, + "rating": 7.8, + "duration": 90, + "genre": "爱情", + "actors_count": 2 + }, + { + "id": 6, + "title": "泰坦尼克号", + "director": "Christopher Nolan", + "year": 2001, + "rating": 6.7, + "duration": 175, + "genre": "喜剧", + "actors_count": 4 + }, + { + "id": 7, + "title": "忠犬八公的故事", + "director": "Lasse Hallström", + "year": 2004, + "rating": 8.2, + "duration": 91, + "genre": "动画", + "actors_count": 3 + }, + { + "id": 8, + "title": "三傻大闹宝莱坞", + "director": "Rajkumar Hirani", + "year": 2011, + "rating": 6.3, + "duration": 175, + "genre": "冒险", + "actors_count": 3 + }, + { + "id": 9, + "title": "阿甘正传", + "director": "Christophe Barratier", + "year": 2022, + "rating": 7.9, + "duration": 107, + "genre": "动画", + "actors_count": 4 + }, + { + "id": 10, + "title": "千与千寻", + "director": "Christopher Nolan", + "year": 1993, + "rating": 7.2, + "duration": 129, + "genre": "悬疑", + "actors_count": 3 + } + ] +} \ No newline at end of file