From 5249ef528eb60aa1bf56635f7b827d676454b83f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E6=A5=A0=E6=A5=A0?= <2509165032@student.example.com> Date: Tue, 23 Jun 2026 11:13:00 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E6=96=87=E4=BB=B6=E8=87=B3?= =?UTF-8?q?=20/?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- q1.py | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ q2 (2).py | 32 ++++++++++++++++++++++++++++++++ q2.py | 32 ++++++++++++++++++++++++++++++++ q4_1.py | 19 +++++++++++++++++++ q4_2.py | 14 ++++++++++++++ 5 files changed, 148 insertions(+) create mode 100644 q1.py create mode 100644 q2 (2).py create mode 100644 q2.py create mode 100644 q4_1.py create mode 100644 q4_2.py diff --git a/q1.py b/q1.py new file mode 100644 index 0000000..74a7bf2 --- /dev/null +++ b/q1.py @@ -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) \ No newline at end of file diff --git a/q2 (2).py b/q2 (2).py new file mode 100644 index 0000000..088ae73 --- /dev/null +++ b/q2 (2).py @@ -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) \ No newline at end of file diff --git a/q2.py b/q2.py new file mode 100644 index 0000000..088ae73 --- /dev/null +++ b/q2.py @@ -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) \ No newline at end of file diff --git a/q4_1.py b/q4_1.py new file mode 100644 index 0000000..aed5034 --- /dev/null +++ b/q4_1.py @@ -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() \ No newline at end of file diff --git a/q4_2.py b/q4_2.py new file mode 100644 index 0000000..18bf9fe --- /dev/null +++ b/q4_2.py @@ -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() \ No newline at end of file