This commit is contained in:
2509165027
2026-06-23 11:22:41 +08:00
parent 1e00974a79
commit 74906f0596
4 changed files with 337 additions and 0 deletions

152
q2_1_crawler/movies.html Normal file
View File

@@ -0,0 +1,152 @@
<!-- exam_fingerprint: B-20260623-2074 -->
<!-- server_time: 2026-06-23 11:13:33 -->
<!-- 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-2074</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>2019</td>
<td>8.2</td>
<td>134</td>
<td>爱情</td>
<td>2</td>
</tr>
<tr>
<td>2</td>
<td>忠犬八公的故事</td>
<td>陈凯歌</td>
<td>2010</td>
<td>6.7</td>
<td>130</td>
<td>爱情</td>
<td>4</td>
</tr>
<tr>
<td>3</td>
<td>星际穿越</td>
<td>Robert Zemeckis</td>
<td>2010</td>
<td>7.7</td>
<td>109</td>
<td>动画</td>
<td>2</td>
</tr>
<tr>
<td>4</td>
<td>阿甘正传</td>
<td>James Cameron</td>
<td>2005</td>
<td>9.2</td>
<td>98</td>
<td>爱情</td>
<td>4</td>
</tr>
<tr>
<td>5</td>
<td>肖申克的救赎</td>
<td>宫崎骏</td>
<td>2022</td>
<td>8.9</td>
<td>111</td>
<td>冒险</td>
<td>4</td>
</tr>
<tr>
<td>6</td>
<td>霸王别姬</td>
<td>Christopher Nolan</td>
<td>2005</td>
<td>9.4</td>
<td>169</td>
<td>悬疑</td>
<td>5</td>
</tr>
<tr>
<td>7</td>
<td>千与千寻</td>
<td>Lasse Hallström</td>
<td>2000</td>
<td>9.2</td>
<td>106</td>
<td>冒险</td>
<td>4</td>
</tr>
<tr>
<td>8</td>
<td>盗梦空间</td>
<td>Rajkumar Hirani</td>
<td>2014</td>
<td>8.0</td>
<td>101</td>
<td>悬疑</td>
<td>2</td>
</tr>
<tr>
<td>9</td>
<td>泰坦尼克号</td>
<td>Christophe Barratier</td>
<td>1996</td>
<td>9.2</td>
<td>93</td>
<td>喜剧</td>
<td>5</td>
</tr>
<tr>
<td>10</td>
<td>三傻大闹宝莱坞</td>
<td>Christopher Nolan</td>
<td>2003</td>
<td>7.9</td>
<td>114</td>
<td>冒险</td>
<td>4</td>
</tr>
</tbody>
</table>
</body>
</html>

102
q2_1_crawler/movies.json Normal file
View File

@@ -0,0 +1,102 @@
[
{
"id": "1",
"title": "放牛班的春天",
"director": "Frank Darabont",
"year": 2019,
"rating": 8.2,
"duration": 134,
"genre": "爱情",
"actors_count": 2
},
{
"id": "2",
"title": "忠犬八公的故事",
"director": "陈凯歌",
"year": 2010,
"rating": 6.7,
"duration": 130,
"genre": "爱情",
"actors_count": 4
},
{
"id": "3",
"title": "星际穿越",
"director": "Robert Zemeckis",
"year": 2010,
"rating": 7.7,
"duration": 109,
"genre": "动画",
"actors_count": 2
},
{
"id": "4",
"title": "阿甘正传",
"director": "James Cameron",
"year": 2005,
"rating": 9.2,
"duration": 98,
"genre": "爱情",
"actors_count": 4
},
{
"id": "5",
"title": "肖申克的救赎",
"director": "宫崎骏",
"year": 2022,
"rating": 8.9,
"duration": 111,
"genre": "冒险",
"actors_count": 4
},
{
"id": "6",
"title": "霸王别姬",
"director": "Christopher Nolan",
"year": 2005,
"rating": 9.4,
"duration": 169,
"genre": "悬疑",
"actors_count": 5
},
{
"id": "7",
"title": "千与千寻",
"director": "Lasse Hallström",
"year": 2000,
"rating": 9.2,
"duration": 106,
"genre": "冒险",
"actors_count": 4
},
{
"id": "8",
"title": "盗梦空间",
"director": "Rajkumar Hirani",
"year": 2014,
"rating": 8.0,
"duration": 101,
"genre": "悬疑",
"actors_count": 2
},
{
"id": "9",
"title": "泰坦尼克号",
"director": "Christophe Barratier",
"year": 1996,
"rating": 9.2,
"duration": 93,
"genre": "喜剧",
"actors_count": 5
},
{
"id": "10",
"title": "三傻大闹宝莱坞",
"director": "Christopher Nolan",
"year": 2003,
"rating": 7.9,
"duration": 114,
"genre": "冒险",
"actors_count": 4
}
]

46
q2_1_crawler/q2_1.py Normal file
View File

@@ -0,0 +1,46 @@
import requests
import json
from bs4 import BeautifulSoup
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'
html_content = response.text
with open('movies.html', 'w', encoding='utf-8') as f_html:
f_html.write(html_content)
soup = BeautifulSoup(html_content, 'html.parser')
table = soup.find('table')
tbody = table.find('tbody')
rows = tbody.find_all('tr')
movies_data = []
for row in rows:
tds = row.find_all('td')
if len(tds) >= 8:
movie = {
"id": tds[0].text.strip(),
"title": tds[1].text.strip(),
"director": tds[2].text.strip(),
"year": int(tds[3].text.strip()),
"rating": float(tds[4].text.strip()),
"duration": int(tds[5].text.strip()),
"genre": tds[6].text.strip(),
"actors_count": int(tds[7].text.strip())
}
movies_data.append(movie)
with open('movies.json', 'w', encoding='utf-8') as f_json:
json.dump(movies_data, f_json, ensure_ascii=False, indent=4)
print(f"爬取成功!共获取 {len(movies_data)} 条电影数据。")
print("文件 movies.html 和 movies.json 已保存。")
except Exception as e:
print(f"爬取或解析失败,错误信息:{e}")

37
q2_1_crawler/q2_2.py Normal file
View File

@@ -0,0 +1,37 @@
import json
with open('q2_1_crawler\movies.json', 'r', encoding='utf-8') as f:
movies = json.load(f)
if movies:
sorted_by_rating = sorted(movies, key=lambda x: x['rating'])
lowest_movie = sorted_by_rating[0]
highest_movie = sorted_by_rating[-1]
print("2.1 最高评分电影:")
print(f"电影名: {highest_movie['title']}, 评分: {highest_movie['rating']}")
print("\n最低评分电影:")
print(f"电影名: {lowest_movie['title']}, 评分: {lowest_movie['rating']}")
genre_counts = {}
for movie in movies:
genre = movie['genre']
genre_counts[genre] = genre_counts.get(genre, 0) + 1
print("\n2.2 各类型电影数量字典:")
print(genre_counts)
director_counts = {}
for movie in movies:
director = movie['director']
director_counts[director] = director_counts.get(director, 0) + 1
print("\n2.3 各导演电影数量字典:")
print(director_counts)
count_2020_later = 0
for movie in movies:
if movie['year'] >= 2020:
count_2020_later += 1
print(f"\n2.4 2020年以后上映的电影数量: {count_2020_later}")