From bc7e68bcddc27042c47763f2dfde843917e36f1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E5=85=B4=E5=AE=87?= <2505155046@student.example.com> Date: Tue, 23 Jun 2026 11:30:01 +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 ++++++++++++++++++++++++++++++++++++ 爬虫.py | 92 +++++++++++++++++++++++++++++++ 3 files changed, 349 insertions(+) create mode 100644 movies.html create mode 100644 movies.json create mode 100644 爬虫.py diff --git a/movies.html b/movies.html new file mode 100644 index 0000000..e545c70 --- /dev/null +++ b/movies.html @@ -0,0 +1,152 @@ + + + + + + + + 电影列表 + + + +

电影列表

+

数据编号:B-20260623-4783

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
编号电影名导演上映年份评分时长(分钟)类型主演数
1千与千寻Frank Darabont20018.491悬疑2
2霸王别姬陈凯歌20029.3125悬疑2
3阿甘正传Robert Zemeckis19999.399喜剧4
4放牛班的春天James Cameron20126.3100爱情3
5星际穿越宫崎骏20246.5166爱情3
6泰坦尼克号Christopher Nolan20037.3127动画5
7盗梦空间Lasse Hallström19939.2118喜剧4
8忠犬八公的故事Rajkumar Hirani20038.9118动画4
9三傻大闹宝莱坞Christophe Barratier20108.3137动画2
10肖申克的救赎Christopher Nolan19998.2178爱情2
+ + \ No newline at end of file diff --git a/movies.json b/movies.json new file mode 100644 index 0000000..dc3cb9c --- /dev/null +++ b/movies.json @@ -0,0 +1,105 @@ +{ + "data_id": null, + "movies": [ + { + "id": 1, + "title": "千与千寻", + "director": "Frank Darabont", + "year": 2001, + "rating": 8.4, + "duration": 91, + "genre": "悬疑", + "actors_count": 2 + }, + { + "id": 2, + "title": "霸王别姬", + "director": "陈凯歌", + "year": 2002, + "rating": 9.3, + "duration": 125, + "genre": "悬疑", + "actors_count": 2 + }, + { + "id": 3, + "title": "阿甘正传", + "director": "Robert Zemeckis", + "year": 1999, + "rating": 9.3, + "duration": 99, + "genre": "喜剧", + "actors_count": 4 + }, + { + "id": 4, + "title": "放牛班的春天", + "director": "James Cameron", + "year": 2012, + "rating": 6.3, + "duration": 100, + "genre": "爱情", + "actors_count": 3 + }, + { + "id": 5, + "title": "星际穿越", + "director": "宫崎骏", + "year": 2024, + "rating": 6.5, + "duration": 166, + "genre": "爱情", + "actors_count": 3 + }, + { + "id": 6, + "title": "泰坦尼克号", + "director": "Christopher Nolan", + "year": 2003, + "rating": 7.3, + "duration": 127, + "genre": "动画", + "actors_count": 5 + }, + { + "id": 7, + "title": "盗梦空间", + "director": "Lasse Hallström", + "year": 1993, + "rating": 9.2, + "duration": 118, + "genre": "喜剧", + "actors_count": 4 + }, + { + "id": 8, + "title": "忠犬八公的故事", + "director": "Rajkumar Hirani", + "year": 2003, + "rating": 8.9, + "duration": 118, + "genre": "动画", + "actors_count": 4 + }, + { + "id": 9, + "title": "三傻大闹宝莱坞", + "director": "Christophe Barratier", + "year": 2010, + "rating": 8.3, + "duration": 137, + "genre": "动画", + "actors_count": 2 + }, + { + "id": 10, + "title": "肖申克的救赎", + "director": "Christopher Nolan", + "year": 1999, + "rating": 8.2, + "duration": 178, + "genre": "爱情", + "actors_count": 2 + } + ] +} \ No newline at end of file diff --git a/爬虫.py b/爬虫.py new file mode 100644 index 0000000..20d4f6d --- /dev/null +++ b/爬虫.py @@ -0,0 +1,92 @@ +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 = "utf-8" +html_text = resp.text + +# 保存原始网页 movies.html +with open("movies.html", "w", encoding="utf-8") as f: + f.write(html_text) +print("✅ 已保存网页源码 movies.html") + +# 解析表格页面 +soup = BeautifulSoup(html_text, "html.parser") + +# 提取页面data_id(页面顶部/body标签,没有则留空) +data_id = None +if soup.body and "data-id" in soup.body.attrs: + data_id = soup.body["data-id"] +print(f"页面data_id: {data_id}") + +# 提取表格所有数据行 ,跳过表头第一行 +all_tr = soup.find_all("tr") +movie_list = [] +# 表头是第0行,电影从第1行开始遍历 +for tr in all_tr[1:]: + td_list = tr.find_all("td") + # 表格列顺序:id、title、director、year、rating、duration、genre、actors_count + if len(td_list) >= 8: + movie = { + "id": int(td_list[0].get_text(strip=True)), + "title": td_list[1].get_text(strip=True), + "director": td_list[2].get_text(strip=True), + "year": int(td_list[3].get_text(strip=True)), + "rating": float(td_list[4].get_text(strip=True)), + "duration": int(td_list[5].get_text(strip=True)), + "genre": td_list[6].get_text(strip=True), + "actors_count": int(td_list[7].get_text(strip=True)) + } + movie_list.append(movie) + +print(f"一共抓取到 {len(movie_list)} 部电影") + +# 组装并保存 movies.json +save_data = { + "data_id": data_id, + "movies": movie_list +} +with open("movies.json", "w", encoding="utf-8") as f: + json.dump(save_data, f, ensure_ascii=False, indent=2) +print("✅ movies.json 写入完成") + +# ====================== 第2题数据分析(防止空列表报错) ====================== +if len(movie_list) == 0: + print("❌ 未抓取到任何电影数据,请检查表格解析逻辑!") +else: + # ① 找出评分最高、最低电影 + sorted_movies = sorted(movie_list, key=lambda x: x["rating"]) + lowest_movie = sorted_movies[0] + highest_movie = sorted_movies[-1] + print("\n① 评分最高&最低电影:") + print(f"评分最低:{lowest_movie['title']} {lowest_movie['rating']}") + print(f"评分最高:{highest_movie['title']} {highest_movie['rating']}") + + # ② 统计各类型电影数量(字典输出) + genre_count = {} + for m in movie_list: + g = m["genre"] + genre_count[g] = genre_count.get(g, 0) + 1 + print("\n② 各类型电影数量:", genre_count) + + # ③ 统计各导演电影数量(字典输出) + director_count = {} + for m in movie_list: + d = m["director"] + director_count[d] = director_count.get(d, 0) + 1 + print("\n③ 各导演电影数量:", director_count) + + # ④ 统计2020年(含)以后上映电影 + count_2020 = 0 + for m in movie_list: + if m["year"] >= 2020: + count_2020 += 1 + print(f"\n④ 2020年(含)后上映电影总数:{count_2020}") \ No newline at end of file