上传文件至 q2_1_crawler
This commit is contained in:
152
q2_1_crawler/movies.html
Normal file
152
q2_1_crawler/movies.html
Normal file
@@ -0,0 +1,152 @@
|
|||||||
|
<!-- exam_fingerprint: B-20260623-8659 -->
|
||||||
|
<!-- server_time: 2026-06-23 10:54:22 -->
|
||||||
|
<!-- 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-8659</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>1998</td>
|
||||||
|
<td>8.3</td>
|
||||||
|
<td>103</td>
|
||||||
|
<td>喜剧</td>
|
||||||
|
<td>3</td>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
<tr>
|
||||||
|
<td>2</td>
|
||||||
|
<td>千与千寻</td>
|
||||||
|
<td>陈凯歌</td>
|
||||||
|
<td>2023</td>
|
||||||
|
<td>7.9</td>
|
||||||
|
<td>118</td>
|
||||||
|
<td>剧情</td>
|
||||||
|
<td>3</td>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
<tr>
|
||||||
|
<td>3</td>
|
||||||
|
<td>泰坦尼克号</td>
|
||||||
|
<td>Robert Zemeckis</td>
|
||||||
|
<td>2018</td>
|
||||||
|
<td>6.8</td>
|
||||||
|
<td>164</td>
|
||||||
|
<td>科幻</td>
|
||||||
|
<td>2</td>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
<tr>
|
||||||
|
<td>4</td>
|
||||||
|
<td>忠犬八公的故事</td>
|
||||||
|
<td>James Cameron</td>
|
||||||
|
<td>2012</td>
|
||||||
|
<td>7.6</td>
|
||||||
|
<td>135</td>
|
||||||
|
<td>爱情</td>
|
||||||
|
<td>5</td>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
<tr>
|
||||||
|
<td>5</td>
|
||||||
|
<td>阿甘正传</td>
|
||||||
|
<td>宫崎骏</td>
|
||||||
|
<td>2011</td>
|
||||||
|
<td>8.8</td>
|
||||||
|
<td>128</td>
|
||||||
|
<td>动画</td>
|
||||||
|
<td>4</td>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
<tr>
|
||||||
|
<td>6</td>
|
||||||
|
<td>霸王别姬</td>
|
||||||
|
<td>Christopher Nolan</td>
|
||||||
|
<td>2022</td>
|
||||||
|
<td>7.2</td>
|
||||||
|
<td>91</td>
|
||||||
|
<td>悬疑</td>
|
||||||
|
<td>5</td>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
<tr>
|
||||||
|
<td>7</td>
|
||||||
|
<td>三傻大闹宝莱坞</td>
|
||||||
|
<td>Lasse Hallström</td>
|
||||||
|
<td>2009</td>
|
||||||
|
<td>9.2</td>
|
||||||
|
<td>179</td>
|
||||||
|
<td>爱情</td>
|
||||||
|
<td>3</td>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
<tr>
|
||||||
|
<td>8</td>
|
||||||
|
<td>肖申克的救赎</td>
|
||||||
|
<td>Rajkumar Hirani</td>
|
||||||
|
<td>1991</td>
|
||||||
|
<td>7.1</td>
|
||||||
|
<td>175</td>
|
||||||
|
<td>喜剧</td>
|
||||||
|
<td>2</td>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
<tr>
|
||||||
|
<td>9</td>
|
||||||
|
<td>盗梦空间</td>
|
||||||
|
<td>Christophe Barratier</td>
|
||||||
|
<td>2024</td>
|
||||||
|
<td>8.8</td>
|
||||||
|
<td>144</td>
|
||||||
|
<td>喜剧</td>
|
||||||
|
<td>5</td>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
<tr>
|
||||||
|
<td>10</td>
|
||||||
|
<td>星际穿越</td>
|
||||||
|
<td>Christopher Nolan</td>
|
||||||
|
<td>2008</td>
|
||||||
|
<td>8.9</td>
|
||||||
|
<td>179</td>
|
||||||
|
<td>动画</td>
|
||||||
|
<td>3</td>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
107
q2_1_crawler/movies.html.py
Normal file
107
q2_1_crawler/movies.html.py
Normal file
@@ -0,0 +1,107 @@
|
|||||||
|
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")
|
||||||
|
|
||||||
|
# 优先尝试直接解析接口JSON(接口真实返回格式)
|
||||||
|
movie_list = []
|
||||||
|
data_id = None
|
||||||
|
try:
|
||||||
|
api_data = json.loads(html_text)
|
||||||
|
data_id = api_data.get("data_id")
|
||||||
|
movie_list = api_data.get("movies", [])
|
||||||
|
print("✅ 识别为JSON接口,直接读取数据")
|
||||||
|
except json.JSONDecodeError:
|
||||||
|
# 若为HTML表格页面,执行原bs4解析逻辑
|
||||||
|
print("识别为HTML表格页面,使用BeautifulSoup解析")
|
||||||
|
soup = BeautifulSoup(html_text, "html.parser")
|
||||||
|
# 提取页面data_id
|
||||||
|
if soup.body and "data-id" in soup.body.attrs:
|
||||||
|
data_id = soup.body["data-id"]
|
||||||
|
# 提取表格行
|
||||||
|
all_tr = soup.find_all("tr")
|
||||||
|
for tr in all_tr[1:]:
|
||||||
|
td_list = tr.find_all("td")
|
||||||
|
if len(td_list) >= 8:
|
||||||
|
# 增加类型转换容错
|
||||||
|
def safe_int(txt):
|
||||||
|
try:
|
||||||
|
return int(txt.strip())
|
||||||
|
except:
|
||||||
|
return 0
|
||||||
|
def safe_float(txt):
|
||||||
|
try:
|
||||||
|
return float(txt.strip())
|
||||||
|
except:
|
||||||
|
return 0.0
|
||||||
|
movie = {
|
||||||
|
"id": safe_int(td_list[0].get_text()),
|
||||||
|
"title": td_list[1].get_text(strip=True),
|
||||||
|
"director": td_list[2].get_text(strip=True),
|
||||||
|
"year": safe_int(td_list[3].get_text()),
|
||||||
|
"rating": safe_float(td_list[4].get_text()),
|
||||||
|
"duration": safe_int(td_list[5].get_text()),
|
||||||
|
"genre": td_list[6].get_text(strip=True),
|
||||||
|
"actors_count": safe_int(td_list[7].get_text())
|
||||||
|
}
|
||||||
|
movie_list.append(movie)
|
||||||
|
|
||||||
|
print(f"页面data_id: {data_id}")
|
||||||
|
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}")
|
||||||
105
q2_1_crawler/movies.json
Normal file
105
q2_1_crawler/movies.json
Normal file
@@ -0,0 +1,105 @@
|
|||||||
|
{
|
||||||
|
"data_id": null,
|
||||||
|
"movies": [
|
||||||
|
{
|
||||||
|
"id": 1,
|
||||||
|
"title": "放牛班的春天",
|
||||||
|
"director": "Frank Darabont",
|
||||||
|
"year": 1998,
|
||||||
|
"rating": 8.3,
|
||||||
|
"duration": 103,
|
||||||
|
"genre": "喜剧",
|
||||||
|
"actors_count": 3
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": 2,
|
||||||
|
"title": "千与千寻",
|
||||||
|
"director": "陈凯歌",
|
||||||
|
"year": 2023,
|
||||||
|
"rating": 7.9,
|
||||||
|
"duration": 118,
|
||||||
|
"genre": "剧情",
|
||||||
|
"actors_count": 3
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": 3,
|
||||||
|
"title": "泰坦尼克号",
|
||||||
|
"director": "Robert Zemeckis",
|
||||||
|
"year": 2018,
|
||||||
|
"rating": 6.8,
|
||||||
|
"duration": 164,
|
||||||
|
"genre": "科幻",
|
||||||
|
"actors_count": 2
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": 4,
|
||||||
|
"title": "忠犬八公的故事",
|
||||||
|
"director": "James Cameron",
|
||||||
|
"year": 2012,
|
||||||
|
"rating": 7.6,
|
||||||
|
"duration": 135,
|
||||||
|
"genre": "爱情",
|
||||||
|
"actors_count": 5
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": 5,
|
||||||
|
"title": "阿甘正传",
|
||||||
|
"director": "宫崎骏",
|
||||||
|
"year": 2011,
|
||||||
|
"rating": 8.8,
|
||||||
|
"duration": 128,
|
||||||
|
"genre": "动画",
|
||||||
|
"actors_count": 4
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": 6,
|
||||||
|
"title": "霸王别姬",
|
||||||
|
"director": "Christopher Nolan",
|
||||||
|
"year": 2022,
|
||||||
|
"rating": 7.2,
|
||||||
|
"duration": 91,
|
||||||
|
"genre": "悬疑",
|
||||||
|
"actors_count": 5
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": 7,
|
||||||
|
"title": "三傻大闹宝莱坞",
|
||||||
|
"director": "Lasse Hallström",
|
||||||
|
"year": 2009,
|
||||||
|
"rating": 9.2,
|
||||||
|
"duration": 179,
|
||||||
|
"genre": "爱情",
|
||||||
|
"actors_count": 3
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": 8,
|
||||||
|
"title": "肖申克的救赎",
|
||||||
|
"director": "Rajkumar Hirani",
|
||||||
|
"year": 1991,
|
||||||
|
"rating": 7.1,
|
||||||
|
"duration": 175,
|
||||||
|
"genre": "喜剧",
|
||||||
|
"actors_count": 2
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": 9,
|
||||||
|
"title": "盗梦空间",
|
||||||
|
"director": "Christophe Barratier",
|
||||||
|
"year": 2024,
|
||||||
|
"rating": 8.8,
|
||||||
|
"duration": 144,
|
||||||
|
"genre": "喜剧",
|
||||||
|
"actors_count": 5
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": 10,
|
||||||
|
"title": "星际穿越",
|
||||||
|
"director": "Christopher Nolan",
|
||||||
|
"year": 2008,
|
||||||
|
"rating": 8.9,
|
||||||
|
"duration": 179,
|
||||||
|
"genre": "动画",
|
||||||
|
"actors_count": 3
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user