期末
This commit is contained in:
102
q2_1_crawler/movie.json
Normal file
102
q2_1_crawler/movie.json
Normal file
@@ -0,0 +1,102 @@
|
|||||||
|
[
|
||||||
|
{
|
||||||
|
"id": 1,
|
||||||
|
"title": "三傻大闹宝莱坞",
|
||||||
|
"director": "Frank Darabont",
|
||||||
|
"year": 2018,
|
||||||
|
"rating": 7.0,
|
||||||
|
"duration": 118,
|
||||||
|
"genre": "动画",
|
||||||
|
"actors_count": 5
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": 2,
|
||||||
|
"title": "霸王别姬",
|
||||||
|
"director": "陈凯歌",
|
||||||
|
"year": 2012,
|
||||||
|
"rating": 7.1,
|
||||||
|
"duration": 119,
|
||||||
|
"genre": "爱情",
|
||||||
|
"actors_count": 4
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": 3,
|
||||||
|
"title": "星际穿越",
|
||||||
|
"director": "Robert Zemeckis",
|
||||||
|
"year": 2015,
|
||||||
|
"rating": 8.8,
|
||||||
|
"duration": 171,
|
||||||
|
"genre": "冒险",
|
||||||
|
"actors_count": 3
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": 4,
|
||||||
|
"title": "肖申克的救赎",
|
||||||
|
"director": "James Cameron",
|
||||||
|
"year": 2017,
|
||||||
|
"rating": 8.2,
|
||||||
|
"duration": 149,
|
||||||
|
"genre": "剧情",
|
||||||
|
"actors_count": 3
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": 5,
|
||||||
|
"title": "阿甘正传",
|
||||||
|
"director": "宫崎骏",
|
||||||
|
"year": 2001,
|
||||||
|
"rating": 7.1,
|
||||||
|
"duration": 163,
|
||||||
|
"genre": "悬疑",
|
||||||
|
"actors_count": 3
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": 6,
|
||||||
|
"title": "泰坦尼克号",
|
||||||
|
"director": "Christopher Nolan",
|
||||||
|
"year": 1996,
|
||||||
|
"rating": 8.6,
|
||||||
|
"duration": 171,
|
||||||
|
"genre": "冒险",
|
||||||
|
"actors_count": 5
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": 7,
|
||||||
|
"title": "放牛班的春天",
|
||||||
|
"director": "Lasse Hallström",
|
||||||
|
"year": 2010,
|
||||||
|
"rating": 7.8,
|
||||||
|
"duration": 126,
|
||||||
|
"genre": "科幻",
|
||||||
|
"actors_count": 2
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": 8,
|
||||||
|
"title": "千与千寻",
|
||||||
|
"director": "Rajkumar Hirani",
|
||||||
|
"year": 2002,
|
||||||
|
"rating": 8.6,
|
||||||
|
"duration": 160,
|
||||||
|
"genre": "悬疑",
|
||||||
|
"actors_count": 5
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": 9,
|
||||||
|
"title": "忠犬八公的故事",
|
||||||
|
"director": "Christophe Barratier",
|
||||||
|
"year": 1997,
|
||||||
|
"rating": 7.9,
|
||||||
|
"duration": 138,
|
||||||
|
"genre": "冒险",
|
||||||
|
"actors_count": 5
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": 10,
|
||||||
|
"title": "盗梦空间",
|
||||||
|
"director": "Christopher Nolan",
|
||||||
|
"year": 2008,
|
||||||
|
"rating": 7.3,
|
||||||
|
"duration": 158,
|
||||||
|
"genre": "爱情",
|
||||||
|
"actors_count": 5
|
||||||
|
}
|
||||||
|
]
|
||||||
152
q2_1_crawler/movies.html
Normal file
152
q2_1_crawler/movies.html
Normal file
@@ -0,0 +1,152 @@
|
|||||||
|
<!-- exam_fingerprint: B-20260623-1192 -->
|
||||||
|
<!-- server_time: 2026-06-23 12:00:42 -->
|
||||||
|
<!-- 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-1192</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>2018</td>
|
||||||
|
<td>7.0</td>
|
||||||
|
<td>118</td>
|
||||||
|
<td>动画</td>
|
||||||
|
<td>5</td>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
<tr>
|
||||||
|
<td>2</td>
|
||||||
|
<td>霸王别姬</td>
|
||||||
|
<td>陈凯歌</td>
|
||||||
|
<td>2012</td>
|
||||||
|
<td>7.1</td>
|
||||||
|
<td>119</td>
|
||||||
|
<td>爱情</td>
|
||||||
|
<td>4</td>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
<tr>
|
||||||
|
<td>3</td>
|
||||||
|
<td>星际穿越</td>
|
||||||
|
<td>Robert Zemeckis</td>
|
||||||
|
<td>2015</td>
|
||||||
|
<td>8.8</td>
|
||||||
|
<td>171</td>
|
||||||
|
<td>冒险</td>
|
||||||
|
<td>3</td>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
<tr>
|
||||||
|
<td>4</td>
|
||||||
|
<td>肖申克的救赎</td>
|
||||||
|
<td>James Cameron</td>
|
||||||
|
<td>2017</td>
|
||||||
|
<td>8.2</td>
|
||||||
|
<td>149</td>
|
||||||
|
<td>剧情</td>
|
||||||
|
<td>3</td>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
<tr>
|
||||||
|
<td>5</td>
|
||||||
|
<td>阿甘正传</td>
|
||||||
|
<td>宫崎骏</td>
|
||||||
|
<td>2001</td>
|
||||||
|
<td>7.1</td>
|
||||||
|
<td>163</td>
|
||||||
|
<td>悬疑</td>
|
||||||
|
<td>3</td>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
<tr>
|
||||||
|
<td>6</td>
|
||||||
|
<td>泰坦尼克号</td>
|
||||||
|
<td>Christopher Nolan</td>
|
||||||
|
<td>1996</td>
|
||||||
|
<td>8.6</td>
|
||||||
|
<td>171</td>
|
||||||
|
<td>冒险</td>
|
||||||
|
<td>5</td>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
<tr>
|
||||||
|
<td>7</td>
|
||||||
|
<td>放牛班的春天</td>
|
||||||
|
<td>Lasse Hallström</td>
|
||||||
|
<td>2010</td>
|
||||||
|
<td>7.8</td>
|
||||||
|
<td>126</td>
|
||||||
|
<td>科幻</td>
|
||||||
|
<td>2</td>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
<tr>
|
||||||
|
<td>8</td>
|
||||||
|
<td>千与千寻</td>
|
||||||
|
<td>Rajkumar Hirani</td>
|
||||||
|
<td>2002</td>
|
||||||
|
<td>8.6</td>
|
||||||
|
<td>160</td>
|
||||||
|
<td>悬疑</td>
|
||||||
|
<td>5</td>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
<tr>
|
||||||
|
<td>9</td>
|
||||||
|
<td>忠犬八公的故事</td>
|
||||||
|
<td>Christophe Barratier</td>
|
||||||
|
<td>1997</td>
|
||||||
|
<td>7.9</td>
|
||||||
|
<td>138</td>
|
||||||
|
<td>冒险</td>
|
||||||
|
<td>5</td>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
<tr>
|
||||||
|
<td>10</td>
|
||||||
|
<td>盗梦空间</td>
|
||||||
|
<td>Christopher Nolan</td>
|
||||||
|
<td>2008</td>
|
||||||
|
<td>7.3</td>
|
||||||
|
<td>158</td>
|
||||||
|
<td>爱情</td>
|
||||||
|
<td>5</td>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
42
q2_1_crawler/q2_1.py
Normal file
42
q2_1_crawler/q2_1.py
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
import requests
|
||||||
|
from bs4 import BeautifulSoup as bs
|
||||||
|
import json
|
||||||
|
header={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/149.0.0.0 Safari/537.36 Edg/149.0.0.0'}
|
||||||
|
url="https://exam.detr.top/exam-b/movies"
|
||||||
|
resp=requests.get(url,headers=header)
|
||||||
|
resp.encoding="utf-8"
|
||||||
|
#print(resp.text)
|
||||||
|
soup=bs(resp.text,"html.parser")
|
||||||
|
#print(soup)
|
||||||
|
items= soup.find_all("tbody")
|
||||||
|
#print(items)
|
||||||
|
with open("movies.html", "w", encoding="utf-8") as f:
|
||||||
|
f.write(resp.text)
|
||||||
|
data=[]
|
||||||
|
for item in items:
|
||||||
|
trs=item.find_all("tr")
|
||||||
|
#print(trs)
|
||||||
|
for tr in trs:
|
||||||
|
tds=tr.find_all("td")
|
||||||
|
#print(tds)
|
||||||
|
id=int(tds[0].get_text(strip=True))
|
||||||
|
title = tds[1].get_text(strip=True)
|
||||||
|
director = tds[2].get_text(strip=True)
|
||||||
|
year = int(tds[3].get_text(strip=True))
|
||||||
|
rating = float(tds[4].get_text(strip=True))
|
||||||
|
duration = int(tds[5].get_text(strip=True))
|
||||||
|
genre = tds[6].get_text(strip=True)
|
||||||
|
actors_count = int(tds[7].get_text(strip=True))
|
||||||
|
data.append ({
|
||||||
|
"id": id,
|
||||||
|
"title": title,
|
||||||
|
"director": director,
|
||||||
|
"year": year,
|
||||||
|
"rating": rating,
|
||||||
|
"duration": duration,
|
||||||
|
"genre": genre,
|
||||||
|
"actors_count": actors_count
|
||||||
|
})
|
||||||
|
print(data)
|
||||||
|
with open("movie.json","w",encoding="utf-8") as f:
|
||||||
|
json.dump(data,f,ensure_ascii=False,indent=4)
|
||||||
42
q2_1_crawler/q2_2.py
Normal file
42
q2_1_crawler/q2_2.py
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
import json
|
||||||
|
|
||||||
|
# 读取json文件
|
||||||
|
with open("movie.json", "r", encoding="utf-8") as f:
|
||||||
|
movie_list = json.load(f)
|
||||||
|
|
||||||
|
# ① 找出评分最高、最低的电影,打印名称+评分
|
||||||
|
max_rating_movie = max(movie_list, key=lambda x: x["rating"])
|
||||||
|
min_rating_movie = min(movie_list, key=lambda x: x["rating"])
|
||||||
|
print("=====① 最高/最低评分电影=====")
|
||||||
|
print(f"最高分电影:{max_rating_movie['title']},评分:{max_rating_movie['rating']}")
|
||||||
|
print(f"最低分电影:{min_rating_movie['title']},评分:{min_rating_movie['rating']}")
|
||||||
|
|
||||||
|
# ② 统计各类型电影数量(字典输出)
|
||||||
|
genre_count = {}
|
||||||
|
for m in movie_list:
|
||||||
|
g = m["genre"]
|
||||||
|
if g in genre_count:
|
||||||
|
genre_count[g] += 1
|
||||||
|
else:
|
||||||
|
genre_count[g] = 1
|
||||||
|
print("\n=====② 各类型电影数量=====")
|
||||||
|
print(genre_count)
|
||||||
|
|
||||||
|
# ③ 统计各导演电影数量(字典输出)
|
||||||
|
director_count = {}
|
||||||
|
for m in movie_list:
|
||||||
|
d = m["director"]
|
||||||
|
if d in director_count:
|
||||||
|
director_count[d] += 1
|
||||||
|
else:
|
||||||
|
director_count[d] = 1
|
||||||
|
print("\n=====③ 各导演电影数量=====")
|
||||||
|
print(director_count)
|
||||||
|
|
||||||
|
# ④ 统计2020年(含)以后上映电影数量
|
||||||
|
new_movie_num = 0
|
||||||
|
for m in movie_list:
|
||||||
|
if m["year"] >= 2020:
|
||||||
|
new_movie_num += 1
|
||||||
|
print("\n=====④ 2020年及以后上映影片总数=====")
|
||||||
|
print(f"数量:{new_movie_num}")
|
||||||
Reference in New Issue
Block a user