上传文件至 q2_1_crawler
This commit is contained in:
88
q2_1_crawler/ii.py
Normal file
88
q2_1_crawler/ii.py
Normal file
@@ -0,0 +1,88 @@
|
|||||||
|
import requests
|
||||||
|
from bs4 import BeautifulSoup as bs
|
||||||
|
import json
|
||||||
|
|
||||||
|
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/149.0.0.0 Safari/537.36 Edg/149.0.0.0',
|
||||||
|
'Referer':'https://exam.detr.top/exam-b/movies'}
|
||||||
|
req = requests.get(url, headers=headers)
|
||||||
|
req.encoding="utf-8"
|
||||||
|
|
||||||
|
data=[]
|
||||||
|
|
||||||
|
soup=bs(req.text,"html.parser")
|
||||||
|
# print(soup)
|
||||||
|
#id, title, director, year, rating, duration, genre, actors-count
|
||||||
|
|
||||||
|
items=soup.select('.item-row')
|
||||||
|
# movie_list=[]
|
||||||
|
for i in range(len(items)):
|
||||||
|
id=items[i].find("td",class_="item-id").get_text()
|
||||||
|
title=items[i].find("td",class_="item-title").get_text().strip()
|
||||||
|
director=items[i].find("td",class_="item-director").get_text().strip()
|
||||||
|
year=items[i].find("td",class_="item-year").get_text()
|
||||||
|
rating=items[i].find("td",class_="item-rating").get_text()
|
||||||
|
duration=items[i].find("td",class_="item-duration").get_text()
|
||||||
|
genre=items[i].find("td",class_="item-genre").get_text()
|
||||||
|
actors_count=items[i].find("td",class_="item-actors-count").get_text()
|
||||||
|
|
||||||
|
data.append({
|
||||||
|
"id":id,
|
||||||
|
"title":title,
|
||||||
|
"director":director,
|
||||||
|
"year":year,
|
||||||
|
"rating":rating,
|
||||||
|
"duration":duration,
|
||||||
|
"genre":genre,
|
||||||
|
"actors_count":actors_count
|
||||||
|
|
||||||
|
})
|
||||||
|
|
||||||
|
print(data)
|
||||||
|
|
||||||
|
# for tr in item:
|
||||||
|
# tds=tr.find_all("td")
|
||||||
|
# tds=list(tds)
|
||||||
|
# # print(tds)
|
||||||
|
# if len(tds)<8:
|
||||||
|
# continue
|
||||||
|
# movie={
|
||||||
|
# "id":tds[0].get_text(strip=True),
|
||||||
|
# "title":tds[1].get_text(strip=True),
|
||||||
|
# "director":tds[2].get_text(strip=True),
|
||||||
|
# "year":tds[3].get_text(strip=True),
|
||||||
|
# "rating":tds[4].get_text(strip=True),
|
||||||
|
# "duration":tds[5].get_text(strip=True),
|
||||||
|
# "genre":tds[6].get_text(strip=True),
|
||||||
|
# "actors_count":tds[7].get_text(strip=True)
|
||||||
|
# }
|
||||||
|
# movie_list.append(movie)
|
||||||
|
# print(movie_list)
|
||||||
|
|
||||||
|
|
||||||
|
with open('movie.json', 'w', encoding='utf-8') as f:
|
||||||
|
json.dump(data, f, ensure_ascii=False, indent=2)
|
||||||
|
|
||||||
|
with open("movie.html","w",encoding='utf-8') as f:
|
||||||
|
json.dump(data, f, ensure_ascii=False, indent=2)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# for i in range(len(items)):
|
||||||
|
# rank=i+1
|
||||||
|
# title=items[i].find("span",class_="title").get_text()
|
||||||
|
# actors=items[i].find("div",class_="bd").get_text().strip()
|
||||||
|
# try:
|
||||||
|
# actors=actors.split("主演:")[1].split("\n")[0]
|
||||||
|
# except:
|
||||||
|
# actors="无"
|
||||||
|
# quote=items[i].find("p",class_="quote").get_text().strip()
|
||||||
|
|
||||||
|
# data.append({
|
||||||
|
# "rank":rank,
|
||||||
|
# "title":title,
|
||||||
|
# "actors":actors,
|
||||||
|
# "quote":quote
|
||||||
|
# })
|
||||||
43
q2_1_crawler/q2_2.py
Normal file
43
q2_1_crawler/q2_2.py
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
# ① 找出评分最高和最低的电影,打印电影名 + 评分。
|
||||||
|
# ② 统计各类型的电影数量,用字典格式输出。
|
||||||
|
# ③ 统计各导演的电影数量,用字典格式输出。
|
||||||
|
# ④ 统计 2020 年(含)以后上映的电影数量。
|
||||||
|
|
||||||
|
import json
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
with open('movie.json', 'r', encoding='utf-8') as f:
|
||||||
|
data=json.load(f)
|
||||||
|
# print(data)
|
||||||
|
|
||||||
|
|
||||||
|
sort_movie=sorted(data,key=lambda x:x["rating"])
|
||||||
|
min=sort_movie[0]
|
||||||
|
max=sort_movie[-1]
|
||||||
|
print("评分最低的电影",min["title"],min["rating"])
|
||||||
|
print("评分最高的电影",max["title"],max["rating"])
|
||||||
|
|
||||||
|
genre_shu={}
|
||||||
|
for g in data:
|
||||||
|
ge=g["genre"]
|
||||||
|
if ge in genre_shu:
|
||||||
|
genre_shu[ge]+=1
|
||||||
|
else:
|
||||||
|
genre_shu[ge]=1
|
||||||
|
print("各类型的电影数量",genre_shu)
|
||||||
|
|
||||||
|
director_shu={}
|
||||||
|
for d in data:
|
||||||
|
di=d["director"]
|
||||||
|
if di in director_shu:
|
||||||
|
director_shu[di]+=1
|
||||||
|
else:
|
||||||
|
director_shu[di]=1
|
||||||
|
print("各导演的电影数量",director_shu)
|
||||||
|
|
||||||
|
a=0
|
||||||
|
for y in data:
|
||||||
|
if int(y["year"]) >= 2020:
|
||||||
|
a+=1
|
||||||
|
print("2020 年(含)以后上映的电影数量",a)
|
||||||
25
q2_1_crawler/sandian.py
Normal file
25
q2_1_crawler/sandian.py
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
import matplotlib.pyplot as plt
|
||||||
|
import json
|
||||||
|
|
||||||
|
rating=[]
|
||||||
|
duration=[]
|
||||||
|
|
||||||
|
with open('movie.json', 'r', encoding='utf-8') as f:
|
||||||
|
data=json.load(f)
|
||||||
|
# print(data)
|
||||||
|
for i in data:
|
||||||
|
rating.append(i["rating"])
|
||||||
|
duration.append(i["duration"])
|
||||||
|
plt.figure(figsize=(12, 8))
|
||||||
|
plt.scatter(duration, rating,
|
||||||
|
c='red',
|
||||||
|
s=80, # 点的大小
|
||||||
|
alpha=0.6, # 透明度
|
||||||
|
edgecolors='white') # 点的边框
|
||||||
|
plt.title('时长与评分关系散点图', fontsize=14)
|
||||||
|
plt.xlabel('时长', fontsize=12)
|
||||||
|
plt.ylabel('评分', fontsize=12)
|
||||||
|
plt.grid(True, linestyle='--', alpha=0.5)
|
||||||
|
plt.show()
|
||||||
|
plt.savefig("q4_2_scatter.png",dpi=150,format='png')
|
||||||
|
|
||||||
21
q2_1_crawler/zhifang_rating.py
Normal file
21
q2_1_crawler/zhifang_rating.py
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
import matplotlib.pyplot as plt
|
||||||
|
import json
|
||||||
|
|
||||||
|
rating=[]
|
||||||
|
|
||||||
|
with open('movie.json', 'r', encoding='utf-8') as f:
|
||||||
|
data=json.load(f)
|
||||||
|
# print(data)
|
||||||
|
for i in data:
|
||||||
|
rating.append(i["rating"])
|
||||||
|
|
||||||
|
plt.figure(figsize=(12,8))
|
||||||
|
plt.hist(rating, # 数据
|
||||||
|
bins=10, # 分成几个柱子
|
||||||
|
color='#3498DB', # 颜色
|
||||||
|
edgecolor='white') # 柱子边框颜色
|
||||||
|
plt.title('评分分布', fontsize=14)
|
||||||
|
plt.xlabel('评分', fontsize=13)
|
||||||
|
plt.grid(True, linestyle='--', alpha=0.5, axis='y')
|
||||||
|
plt.show()
|
||||||
|
plt.savefig("q4_3a_hist.png",dpi=150,format='png')
|
||||||
Reference in New Issue
Block a user