上传文件至 /

This commit is contained in:
2026-06-23 11:13:00 +08:00
parent 7282f0159c
commit 5249ef528e
5 changed files with 148 additions and 0 deletions

51
q1.py Normal file
View File

@@ -0,0 +1,51 @@
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"
}
response = requests.get(url, headers=headers)
response.encoding = "utf-8"
html_text = response.text
with open("movies.html", "w", encoding="utf-8") as f:
f.write(html_text)
soup = BeautifulSoup(html_text, "html.parser")
movies_data = []
data_id = ""
table = soup.find("table")
if table:
rows = table.find_all("tr")
for row in rows[1:]:
cols = row.find_all("td")
if len(cols) >= 8:
movie = {
"id": cols[0].text.strip(),
"title": cols[1].text.strip(),
"director": cols[2].text.strip(),
"year": int(cols[3].text.strip()),
"rating": float(cols[4].text.strip()),
"duration": int(cols[5].text.strip()),
"genre": cols[6].text.strip(),
"actors_count": int(cols[7].text.strip())
}
movies_data.append(movie)
data_id_tag = soup.find("span", class_="data-id")
if data_id_tag:
data_id = data_id_tag.text.strip()
result = {
"data_id": data_id,
"movies": movies_data
}
with open("movies.json", "w", encoding="utf-8") as f:
json.dump(result, f, ensure_ascii=False, indent=4)

32
q2 (2).py Normal file
View File

@@ -0,0 +1,32 @@
import json
with open("movies.json", "r", encoding="utf-8") as f:
data = json.load(f)
movies = data["movies"]
# 1.① 评分最高和最低
max_rating_movie = max(movies, key=lambda x: x["rating"])
min_rating_movie = min(movies, key=lambda x: x["rating"])
print(max_rating_movie["title"], max_rating_movie["rating"])
print(min_rating_movie["title"], min_rating_movie["rating"])
# 2.② 各类型电影数量
genre_count = {}
for movie in movies:
genre = movie["genre"]
genre_count[genre] = genre_count.get(genre, 0) + 1
print(genre_count)
# 3.③ 各导演电影数量
director_count = {}
for movie in movies:
director = movie["director"]
director_count[director] = director_count.get(director, 0) + 1
print(director_count)
# 4.④ 2020年以后电影数量
count_2020_later = 0
for movie in movies:
if movie["year"] >= 2020:
count_2020_later += 1
print(count_2020_later)

32
q2.py Normal file
View File

@@ -0,0 +1,32 @@
import json
with open("movies.json", "r", encoding="utf-8") as f:
data = json.load(f)
movies = data["movies"]
# 1.① 评分最高和最低
max_rating_movie = max(movies, key=lambda x: x["rating"])
min_rating_movie = min(movies, key=lambda x: x["rating"])
print(max_rating_movie["title"], max_rating_movie["rating"])
print(min_rating_movie["title"], min_rating_movie["rating"])
# 2.② 各类型电影数量
genre_count = {}
for movie in movies:
genre = movie["genre"]
genre_count[genre] = genre_count.get(genre, 0) + 1
print(genre_count)
# 3.③ 各导演电影数量
director_count = {}
for movie in movies:
director = movie["director"]
director_count[director] = director_count.get(director, 0) + 1
print(director_count)
# 4.④ 2020年以后电影数量
count_2020_later = 0
for movie in movies:
if movie["year"] >= 2020:
count_2020_later += 1
print(count_2020_later)

19
q4_1.py Normal file
View File

@@ -0,0 +1,19 @@
import json
import matplotlib.pyplot as plt
with open("../q2_1_crawler/movies.json","r",encoding="utf-8") as f:
data = json.load(f)
movies = data["movies"]
genre_cnt = {}
for m in movies:
g = m["genre"]
genre_cnt[g] = genre_cnt.get(g,0)+1
x = list(genre_cnt.keys())
y = list(genre_cnt.values())
plt.bar(x,y)
plt.tight_layout()
plt.savefig("q4_1_bar.png",dpi=150)
plt.close()

14
q4_2.py Normal file
View File

@@ -0,0 +1,14 @@
import json
import matplotlib.pyplot as plt
with open("../q2_1_crawler/movies.json","r",encoding="utf-8") as f:
data = json.load(f)
movies = data["movies"]
rating = [i["rating"] for i in movies]
duration = [i["duration"] for i in movies]
plt.scatter(duration,rating)
plt.tight_layout()
plt.savefig("q4_2_scatter.png",dpi=150)
plt.close()