上传文件至 q2_1_crawler

This commit is contained in:
2026-06-23 11:29:07 +08:00
parent 700d4ff3e8
commit 941bdec324
4 changed files with 243 additions and 0 deletions

152
q2_1_crawler/movies.html Normal file
View File

@@ -0,0 +1,152 @@
<!-- exam_fingerprint: B-20260623-4047 -->
<!-- server_time: 2026-06-23 10:30:07 -->
<!-- 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-4047</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>1996</td>
<td>6.2</td>
<td>109</td>
<td>爱情</td>
<td>4</td>
</tr>
<tr>
<td>2</td>
<td>阿甘正传</td>
<td>陈凯歌</td>
<td>1994</td>
<td>8.1</td>
<td>152</td>
<td>喜剧</td>
<td>2</td>
</tr>
<tr>
<td>3</td>
<td>千与千寻</td>
<td>Robert Zemeckis</td>
<td>2000</td>
<td>7.9</td>
<td>97</td>
<td>科幻</td>
<td>4</td>
</tr>
<tr>
<td>4</td>
<td>泰坦尼克号</td>
<td>James Cameron</td>
<td>2000</td>
<td>7.1</td>
<td>104</td>
<td>喜剧</td>
<td>2</td>
</tr>
<tr>
<td>5</td>
<td>肖申克的救赎</td>
<td>宫崎骏</td>
<td>2015</td>
<td>6.6</td>
<td>106</td>
<td>爱情</td>
<td>2</td>
</tr>
<tr>
<td>6</td>
<td>放牛班的春天</td>
<td>Christopher Nolan</td>
<td>2000</td>
<td>7.1</td>
<td>121</td>
<td>喜剧</td>
<td>3</td>
</tr>
<tr>
<td>7</td>
<td>星际穿越</td>
<td>Lasse Hallström</td>
<td>1991</td>
<td>6.3</td>
<td>94</td>
<td>剧情</td>
<td>3</td>
</tr>
<tr>
<td>8</td>
<td>霸王别姬</td>
<td>Rajkumar Hirani</td>
<td>2016</td>
<td>7.6</td>
<td>128</td>
<td>动画</td>
<td>2</td>
</tr>
<tr>
<td>9</td>
<td>忠犬八公的故事</td>
<td>Christophe Barratier</td>
<td>1995</td>
<td>8.4</td>
<td>143</td>
<td>科幻</td>
<td>3</td>
</tr>
<tr>
<td>10</td>
<td>三傻大闹宝莱坞</td>
<td>Christopher Nolan</td>
<td>2019</td>
<td>6.4</td>
<td>137</td>
<td>剧情</td>
<td>2</td>
</tr>
</tbody>
</table>
</body>
</html>

4
q2_1_crawler/movies.json Normal file
View File

@@ -0,0 +1,4 @@
{
"data_id": "unknown",
"movies": []
}

56
q2_1_crawler/q2_1.py Normal file
View File

@@ -0,0 +1,56 @@
import requests
import json
from bs4 import BeautifulSoup
def crawl_movies():
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/91.0.4472.124 Safari/537.36"
}
resp = requests.get(url, headers=headers)
resp.encoding = 'utf-8' # 根据实际编码调整
html = resp.text
# 保存原始网页源码
with open("movies.html", "w", encoding="utf-8") as f:
f.write(html)
# 解析 HTML 提取数据
soup = BeautifulSoup(html, 'html.parser')
# 示例:数据编号(请根据实际网页结构调整选择器)
data_id_elem = soup.find('span', id='data-id')
data_id = data_id_elem.text.strip() if data_id_elem else "unknown"
# 示例:电影列表(请根据实际网页结构调整选择器)
movies = []
movie_elements = soup.find_all('div', class_='movie') # 示例选择器
for elem in movie_elements:
# 以下字段提取均为示例,需实际调整
movie = {
'id': elem.get('data-id') or elem.find('span', class_='id').text.strip(),
'title': elem.find('h3', class_='title').text.strip(),
'director': elem.find('span', class_='director').text.strip(),
'year': int(elem.find('span', class_='year').text.strip()),
'rating': float(elem.find('span', class_='rating').text.strip()),
'duration': int(elem.find('span', class_='duration').text.strip().replace(' min', '')),
'genre': [g.text.strip() for g in elem.find_all('span', class_='genre')],
'actors_count': int(elem.find('span', class_='actors_count').text.strip())
}
movies.append(movie)
# 保存 JSON
result = {
"data_id": data_id,
"movies": movies
}
with open("movies.json", "w", encoding="utf-8") as f:
json.dump(result, f, ensure_ascii=False, indent=2)
print("爬取完成,已生成 movies.html 和 movies.json")
return result
if __name__ == "__main__":
crawl_movies()

31
q2_1_crawler/q2_2.py Normal file
View File

@@ -0,0 +1,31 @@
import json
from collections import Counter
def analyze_movies(json_path="movies.json"):
with open(json_path, "r", encoding="utf-8") as f:
data = json.load(f)
movies = data["movies"]
# ① 评分最高和最低
max_movie = max(movies, key=lambda x: x['rating'])
min_movie = min(movies, key=lambda x: x['rating'])
print(f"最高评分电影:{max_movie['title']},评分:{max_movie['rating']}")
print(f"最低评分电影:{min_movie['title']},评分:{min_movie['rating']}")
# ② 各类型电影数量
genre_counter = Counter()
for movie in movies:
for genre in movie['genre']:
genre_counter[genre] += 1
print("各类型电影数量:", dict(genre_counter))
# ③ 各导演电影数量
director_counter = Counter(movie['director'] for movie in movies)
print("各导演电影数量:", dict(director_counter))
# ④ 2020年以后上映的电影数量
count_2020_plus = sum(1 for movie in movies if movie['year'] >= 2020)
print(f"2020年以后上映的电影数量{count_2020_plus}")
if __name__ == "__main__":
analyze_movies()