上传文件至 /
This commit is contained in:
46
2.py
Normal file
46
2.py
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
import json
|
||||||
|
from collections import defaultdict
|
||||||
|
|
||||||
|
# 读取数据
|
||||||
|
with open("movies.json", "r", encoding="utf-8") as f:
|
||||||
|
data = json.load(f)
|
||||||
|
movies = data.get("movies", [])
|
||||||
|
|
||||||
|
# ① 找出评分最高和最低的电影
|
||||||
|
if movies:
|
||||||
|
# 按rating排序,处理可能的空值
|
||||||
|
sorted_by_rating = sorted(movies, key=lambda x: float(x["rating"]) if x["rating"] else 0)
|
||||||
|
lowest = sorted_by_rating[0]
|
||||||
|
highest = sorted_by_rating[-1]
|
||||||
|
print("评分最低:", lowest["title"], lowest["rating"])
|
||||||
|
print("评分最高:", highest["title"], highest["rating"])
|
||||||
|
else:
|
||||||
|
print("无电影数据")
|
||||||
|
|
||||||
|
# ② 统计各类型的电影数量(注意genre可能是列表或字符串,这里按常见的逗号分隔处理)
|
||||||
|
genre_count = defaultdict(int)
|
||||||
|
for movie in movies:
|
||||||
|
genres = movie["genre"]
|
||||||
|
# 如果genre是字符串,按逗号分割;如果是列表,直接遍历
|
||||||
|
if isinstance(genres, str):
|
||||||
|
genres = [g.strip() for g in genres.split(",")]
|
||||||
|
for g in genres:
|
||||||
|
genre_count[g] += 1
|
||||||
|
print("\n各类型电影数量:", dict(genre_count))
|
||||||
|
|
||||||
|
# ③ 统计各导演的电影数量
|
||||||
|
director_count = defaultdict(int)
|
||||||
|
for movie in movies:
|
||||||
|
director = movie["director"]
|
||||||
|
director_count[director] += 1
|
||||||
|
print("\n各导演电影数量:", dict(director_count))
|
||||||
|
|
||||||
|
# ④ 统计2020年(含)以后上映的电影数量
|
||||||
|
count_2020 = 0
|
||||||
|
for movie in movies:
|
||||||
|
year = movie["year"]
|
||||||
|
if isinstance(year, (int, float)) and year >= 2020:
|
||||||
|
count_2020 += 1
|
||||||
|
elif isinstance(year, str) and year.isdigit() and int(year) >= 2020:
|
||||||
|
count_2020 += 1
|
||||||
|
print(f"\n2020年(含)以后上映的电影数量:{count_2020}")
|
||||||
55
text.py
Normal file
55
text.py
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
import requests
|
||||||
|
import json
|
||||||
|
|
||||||
|
# 目标网址
|
||||||
|
url = "https://exam.detr.top/exam-b/movies"
|
||||||
|
|
||||||
|
# 发送请求获取网页内容
|
||||||
|
response = requests.get(url)
|
||||||
|
response.encoding = "utf-8" # 避免中文乱码
|
||||||
|
|
||||||
|
# 1. 保存原始网页源码为 movies.html
|
||||||
|
with open("movies.html", "w", encoding="utf-8") as f:
|
||||||
|
f.write(response.text)
|
||||||
|
|
||||||
|
# 假设网页返回的是包含数据编号和电影列表的JSON(若实际结构不同,可根据网页返回调整)
|
||||||
|
# 先解析响应内容(如果网页是HTML+JS渲染,可能需要用BeautifulSoup提取数据)
|
||||||
|
# 这里先按题目要求,提取数据编号和10部电影信息
|
||||||
|
# 注意:如果网页是静态HTML,需用BeautifulSoup解析;如果是直接返回JSON,直接json.loads即可
|
||||||
|
|
||||||
|
# 方式1:如果网页直接返回JSON(示例,需根据实际网页结构调整)
|
||||||
|
try:
|
||||||
|
data = json.loads(response.text)
|
||||||
|
except json.JSONDecodeError:
|
||||||
|
# 方式2:如果是HTML,用BeautifulSoup提取数据(这里给通用模板)
|
||||||
|
from bs4 import BeautifulSoup
|
||||||
|
soup = BeautifulSoup(response.text, "html.parser")
|
||||||
|
# 假设数据在script标签中,或直接在HTML表格/列表中,需根据实际结构提取
|
||||||
|
# 这里为了适配题目,先模拟数据结构(实际使用时替换为真实提取逻辑)
|
||||||
|
data = {
|
||||||
|
"data_id": "demo_id", # 数据编号,根据网页实际获取
|
||||||
|
"movies": [] # 10部电影列表,每部包含题目要求的键
|
||||||
|
}
|
||||||
|
|
||||||
|
# 提取数据编号和电影信息(按题目要求的键)
|
||||||
|
result = {
|
||||||
|
"data_id": data.get("data_id", ""),
|
||||||
|
"movies": []
|
||||||
|
}
|
||||||
|
for movie in data.get("movies", []):
|
||||||
|
# 按题目要求保留指定键
|
||||||
|
filtered_movie = {
|
||||||
|
"id": movie.get("id"),
|
||||||
|
"title": movie.get("title"),
|
||||||
|
"director": movie.get("director"),
|
||||||
|
"year": movie.get("year"),
|
||||||
|
"rating": movie.get("rating"),
|
||||||
|
"duration": movie.get("duration"),
|
||||||
|
"genre": movie.get("genre"),
|
||||||
|
"actors_count": movie.get("actors_count")
|
||||||
|
}
|
||||||
|
result["movies"].append(filtered_movie)
|
||||||
|
|
||||||
|
# 保存为 movies.json
|
||||||
|
with open("movies.json", "w", encoding="utf-8") as f:
|
||||||
|
json.dump(result, f, ensure_ascii=False, indent=4)
|
||||||
Reference in New Issue
Block a user