From 6f36d4b67a0123cfacb1d858a7505679096c7947 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E4=BD=99=E9=9B=AF=E8=90=B1?=
<2509165035@student.example.com>
Date: Thu, 2 Apr 2026 16:05:52 +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
---
35.py | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 70 insertions(+)
create mode 100644 35.py
diff --git a/35.py b/35.py
new file mode 100644
index 0000000..9993ef4
--- /dev/null
+++ b/35.py
@@ -0,0 +1,70 @@
+import requests
+import re
+import csv
+import json
+
+url = "https://movie.douban.com/top250"
+headers = {
+ "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36"
+}
+
+response = requests.get(url, headers=headers)
+html = response.text
+
+pattern = re.compile(
+ r'(\d+).*?'
+ r'(.*?).*?'
+ r'(.*?).*?'-
+ r'(.*?).*?'
+ r'(\d+人评价).*?'
+ r'(.*?)?',
+ re.S
+)
+
+movies = pattern.findall(html)[:10]
+
+movie_list = []
+for m in movies:
+ rank = m[0]
+ title = m[1]
+ en_title = m[2].replace("/", "").strip()
+ rating = m[3]
+ people = m[4]
+ quote = m[5] if len(m) > 5 else ""
+ movie_list.append({
+ "rank": int(rank),
+ "title": title,
+ "en_title": en_title,
+ "rating": rating,
+ "people": people,
+ "quote": quote
+ })
+
+with open("movies.txt", "w", encoding="utf-8") as f:
+ for m in movie_list:
+ f.write(m["title"] + " | " + m["quote"] + "\n")
+print("✅ 练习1完成:已保存中文名+评语到 movies.txt")
+
+with open("movies.csv", "w", encoding="utf-8-sig", newline="") as f:
+ writer = csv.writer(f)
+ writer.writerow(["排名", "中文名", "英文名", "评分", "评价人数", "经典评语"])
+ for m in movie_list:
+ writer.writerow([m["rank"], m["title"], m["en_title"], m["rating"], m["people"], m["quote"]])
+print("✅ 练习2完成:已保存到 movies.csv")
+
+with open("movies.json", "w", encoding="utf-8") as f:
+ json.dump(movie_list, f, ensure_ascii=False, indent=4)
+print("✅ 练习3完成:已保存到 movies.json")
+
+print("\n--- 练习4:筛选评分>9.5的电影 ---")
+with open("movies.csv", "r", encoding="utf-8-sig") as f:
+ reader = csv.DictReader(f)
+ for row in reader:
+ if float(row["评分"]) > 9.5:
+ print(row)
+
+print("\n--- 练习5:JSON统计 ---")
+with open("movies.json", "r", encoding="utf-8") as f:
+ data = json.load(f)
+
+