This commit is contained in:
2509165028
2026-06-23 11:16:59 +08:00
parent 0ee17b1140
commit 495d119600
4 changed files with 249 additions and 0 deletions

152
q2_1_crawler/movies.html Normal file
View File

@@ -0,0 +1,152 @@
<!-- exam_fingerprint: B-20260623-8741 -->
<!-- server_time: 2026-06-23 10:13:12 -->
<!-- exam_paper: B -->
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>电影列表</title>
<style>
body { font-family: "Microsoft YaHei", sans-serif; margin: 20px; background: #f5f5f5; }
h1 { color: #c0392b; }
.meta { color: #999; font-size: 12px; margin-bottom: 15px; }
.meta code { background: #e9ecef; padding: 2px 6px; border-radius: 3px; }
table { width: 100%; border-collapse: collapse; background: white; box-shadow: 0 1px 3px rgba(0,0,0,0.1); }
th, td { padding: 10px; text-align: left; border-bottom: 1px solid #ddd; }
th { background: #c0392b; color: white; }
tr:hover { background: #fef5f4; }
</style>
</head>
<body>
<h1>电影列表</h1>
<p class="meta">数据编号:<code>B-20260623-8741</code></p>
<table>
<thead>
<tr>
<th>编号</th>
<th>电影名</th>
<th>导演</th>
<th>上映年份</th>
<th>评分</th>
<th>时长(分钟)</th>
<th>类型</th>
<th>主演数</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>盗梦空间</td>
<td>Frank Darabont</td>
<td>2024</td>
<td>7.1</td>
<td>126</td>
<td>悬疑</td>
<td>2</td>
</tr>
<tr>
<td>2</td>
<td>放牛班的春天</td>
<td>陈凯歌</td>
<td>2013</td>
<td>7.8</td>
<td>162</td>
<td>悬疑</td>
<td>3</td>
</tr>
<tr>
<td>3</td>
<td>三傻大闹宝莱坞</td>
<td>Robert Zemeckis</td>
<td>2004</td>
<td>9.1</td>
<td>179</td>
<td>爱情</td>
<td>2</td>
</tr>
<tr>
<td>4</td>
<td>泰坦尼克号</td>
<td>James Cameron</td>
<td>2006</td>
<td>8.1</td>
<td>172</td>
<td>爱情</td>
<td>4</td>
</tr>
<tr>
<td>5</td>
<td>肖申克的救赎</td>
<td>宫崎骏</td>
<td>2002</td>
<td>6.0</td>
<td>153</td>
<td>冒险</td>
<td>5</td>
</tr>
<tr>
<td>6</td>
<td>千与千寻</td>
<td>Christopher Nolan</td>
<td>2017</td>
<td>8.0</td>
<td>163</td>
<td>爱情</td>
<td>3</td>
</tr>
<tr>
<td>7</td>
<td>星际穿越</td>
<td>Lasse Hallström</td>
<td>2021</td>
<td>9.5</td>
<td>148</td>
<td>冒险</td>
<td>3</td>
</tr>
<tr>
<td>8</td>
<td>忠犬八公的故事</td>
<td>Rajkumar Hirani</td>
<td>2006</td>
<td>7.3</td>
<td>115</td>
<td>动画</td>
<td>3</td>
</tr>
<tr>
<td>9</td>
<td>霸王别姬</td>
<td>Christophe Barratier</td>
<td>2010</td>
<td>6.6</td>
<td>136</td>
<td>喜剧</td>
<td>2</td>
</tr>
<tr>
<td>10</td>
<td>阿甘正传</td>
<td>Christopher Nolan</td>
<td>2001</td>
<td>9.1</td>
<td>107</td>
<td>喜剧</td>
<td>4</td>
</tr>
</tbody>
</table>
</body>
</html>

22
q2_1_crawler/movies.json Normal file
View File

@@ -0,0 +1,22 @@
[
{
"id": "1",
"title": "示例电影A",
"director": "导演A",
"year": 2022,
"rating": 8.5,
"duration": 120,
"genre": "动作",
"actors_count": 4
},
{
"id": "2",
"title": "示例电影B",
"director": "导演B",
"year": 2019,
"rating": 6.0,
"duration": 95,
"genre": "喜剧",
"actors_count": 6
}
]

44
q2_1_crawler/q2_1.py Normal file
View File

@@ -0,0 +1,44 @@
import requests
import json
from bs4 import BeautifulSoup
# 1. 必须包含检测头
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
}
url = 'https://exam.detr.top/exam-b/movies'
try:
response = requests.get(url, headers=headers)
response.encoding = 'utf-8'
# 2. 保存原始网页源码
with open('movies.html', 'w', encoding='utf-8') as f_html:
f_html.write(response.text)
# 3. 解析数据 (这里假设网页是表格或列表,需要手动调试定位)
soup = BeautifulSoup(response.text, 'lxml')
# 示例:假设电影信息在一个 class 为 'movie-item' 的 div 中,实际需要你按 F12 查看源码修改
# movie_items = soup.select('.movie-item')
# 获取前10条或全部
# movies_list = []
# for i, item in enumerate(movie_items[:10]):
# # 根据实际标签提取id, title, director, year, rating, duration, genre, actors_count
# pass
# ⚠️ 由于无法访问在线真实网页此处提供解析后构造的示例数据仅供保存json逻辑参考
# 你实际做的时候,需要将上面的 `movies_list` 替换为真实的爬取结果。
movies_list = [
{"id": "1", "title": "示例电影A", "director": "导演A", "year": 2022, "rating": 8.5, "duration": 120, "genre": "动作", "actors_count": 4},
{"id": "2", "title": "示例电影B", "director": "导演B", "year": 2019, "rating": 6.0, "duration": 95, "genre": "喜剧", "actors_count": 6}
] # 这里要保证爬够10条
# 4. 保存为 movies.json
with open('movies.json', 'w', encoding='utf-8') as f_json:
json.dump(movies_list, f_json, ensure_ascii=False, indent=4)
print("爬取完成,已保存 movies.json 和 movies.html")
except Exception as e:
print(f"爬取失败: {e}")

31
q2_1_crawler/q2_2.py Normal file
View File

@@ -0,0 +1,31 @@
import json
# 1. 读取数据
with open('movies.json', 'r', encoding='utf-8') as f:
movies = json.load(f)
# 2. ① 找出评分最高和最低的电影
max_rating_movie = max(movies, key=lambda x: x['rating'])
min_rating_movie = min(movies, key=lambda x: x['rating'])
print(f"评分最高电影: {max_rating_movie['title']} - {max_rating_movie['rating']}")
print(f"评分最低电影: {min_rating_movie['title']} - {min_rating_movie['rating']}")
# 3. ② 统计各类型的电影数量 (字典格式)
genre_counts = {}
for movie in movies:
genre = movie['genre']
genre_counts[genre] = genre_counts.get(genre, 0) + 1
print("各类型电影数量统计:")
print(json.dumps(genre_counts, ensure_ascii=False, indent=4))
# 4. ③ 统计各导演的电影数量 (字典格式)
director_counts = {}
for movie in movies:
director = movie['director']
director_counts[director] = director_counts.get(director, 0) + 1
print("各导演电影数量统计:")
print(json.dumps(director_counts, ensure_ascii=False, indent=4))
# 5. ④ 统计2020年以后上映的电影数量
count_post_2020 = sum(1 for movie in movies if movie['year'] >= 2020)
print(f"2020年(含)以后上映的电影数量: {count_post_2020}")