From 16c93148d80d625ead7a7a53c3810923be7be0f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=96=E6=AC=A3=E8=8C=B9?= <2509165049@student.example.com> Date: Thu, 2 Apr 2026 15:49:36 +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 --- text | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ text (2) | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 138 insertions(+) create mode 100644 text create mode 100644 text (2) diff --git a/text b/text new file mode 100644 index 0000000..5cb9a04 --- /dev/null +++ b/text @@ -0,0 +1,69 @@ +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--- 练习5:JSON统计 ---") +with open("movies.json", "r", encoding="utf-8") as f: + data = json.load(f) + + +max_movie = max(data, key=lambda x: float(x["rating"])) +print(f"评分最高:{max_movie['title']}({max_movie['rating']}分 | {max_movie['people']})") \ No newline at end of file diff --git a/text (2) b/text (2) new file mode 100644 index 0000000..5cb9a04 --- /dev/null +++ b/text (2) @@ -0,0 +1,69 @@ +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--- 练习5:JSON统计 ---") +with open("movies.json", "r", encoding="utf-8") as f: + data = json.load(f) + + +max_movie = max(data, key=lambda x: float(x["rating"])) +print(f"评分最高:{max_movie['title']}({max_movie['rating']}分 | {max_movie['people']})") \ No newline at end of file