From cb700f4e9a154dc33d89b4f684fcccc58a3120b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E9=92=A6=E7=87=95?= <2509165038@student.example.com> Date: Sat, 4 Jul 2026 02:46:57 +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 --- 1.py | 30 ++++++++++++++++++++++++++++++ q4_1.py | 19 +++++++++++++++++++ q4_2.py | 17 +++++++++++++++++ q4_3a.py | 14 ++++++++++++++ q4_3b.py | 14 ++++++++++++++ 5 files changed, 94 insertions(+) create mode 100644 1.py create mode 100644 q4_1.py create mode 100644 q4_2.py create mode 100644 q4_3a.py create mode 100644 q4_3b.py diff --git a/1.py b/1.py new file mode 100644 index 0000000..f319e96 --- /dev/null +++ b/1.py @@ -0,0 +1,30 @@ +import re +import requests +from bs4 import BeautifulSoup as bs +import json +header = {'User-Agent':'Mozilla/5.0'} +url = 'https://exam.detr.top/exam-b/movies' + +html = requests.get(url, headers=header).text +# print(html) +open('movies.html','w',encoding='utf-8').write(html) +# print(html) +# print('==============') +# fd = bs(html, 'html.parser').find('p', class_='meta') +fd = re.search(r'exam_fingerprint:\s*(\S+)',html).group(1) +# print(fd) + +resp = bs(html, 'html.parser').find_all('tr', class_='item-row') + +# print(resp) +movies = [] + +for i in resp: + c = [] + for r in i.find_all('td'): + c.append(r.text.strip()) + movies.append({ + 'id':int(c[0]), 'title':c[1], 'director':c[2], 'year':int(c[3]), 'rating':float(c[4]), 'duration':int(c[5]), 'genre':c[6], 'actors_count':int(c[7]) + }) + +json.dump({'ID':fd, 'movies':movies}, open('movies.json', 'w', encoding='utf-8'), ensure_ascii=False, indent=2) diff --git a/q4_1.py b/q4_1.py new file mode 100644 index 0000000..b329ba7 --- /dev/null +++ b/q4_1.py @@ -0,0 +1,19 @@ +import json +import matplotlib.pyplot as plt + +movies = json.load(open('movies.json', encoding='utf-8'))['movies'] +# print(movies) +genres = {} +for m in movies: + g = m['genre'] + if g in genres: + genres[g] = genres[g] + 1 + else: + genres[g] = 1 +print(genres) +plt.figure(figsize=(8,5)) +plt.bar(genres.keys(), genres.values()) +plt.title('类型电影数量分布') +plt.xlabel('类型') +plt.ylabel('数量') +plt.savefig('q4_1_bar.png', dpi=150) \ No newline at end of file diff --git a/q4_2.py b/q4_2.py new file mode 100644 index 0000000..ec9d996 --- /dev/null +++ b/q4_2.py @@ -0,0 +1,17 @@ +import json +import matplotlib.pyplot as plt + +movies = json.load(open('movies.json', encoding='utf-8'))['movies'] +# print(movies) +ratings = [] +durations = [] +for m in movies: + ratings.append(m['rating']) + durations.append(m['duration']) + +plt.figure(figsize=(8,5)) +plt.scatter(durations,ratings,color='red',alpha=0.6) +plt.title('时长与评分关系散点图') +plt.xlabel('duration') +plt.ylabel('rating') +plt.savefig('q4_2_scatter.png', dpi=150) \ No newline at end of file diff --git a/q4_3a.py b/q4_3a.py new file mode 100644 index 0000000..ff6357c --- /dev/null +++ b/q4_3a.py @@ -0,0 +1,14 @@ +import json +import matplotlib.pyplot as plt + +movies = json.load(open('movies.json', encoding='utf-8'))['movies'] +# print(movies) +ratings = [] +durations = [] +for m in movies: + ratings.append(m['rating']) + durations.append(m['duration']) + +plt.figure(figsize=(8,5)) +plt.hist(ratings, bins=5,color='blue') +plt.savefig('q4_3a_hist.png',dpi=150) diff --git a/q4_3b.py b/q4_3b.py new file mode 100644 index 0000000..2e12382 --- /dev/null +++ b/q4_3b.py @@ -0,0 +1,14 @@ +import json +import matplotlib.pyplot as plt + +movies = json.load(open('movies.json', encoding='utf-8'))['movies'] +# print(movies) +ratings = [] +durations = [] +for m in movies: + ratings.append(m['rating']) + durations.append(m['duration']) + +plt.figure(figsize=(8,5)) +plt.hist(durations, bins=5,color='blue') +plt.savefig('q4_3b_hist.png',dpi=150) \ No newline at end of file