上传文件至 /

This commit is contained in:
2026-04-02 16:05:52 +08:00
parent 2848230419
commit 6f36d4b67a

70
35.py Normal file
View File

@@ -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'<em class="">(\d+)</em>.*?'
r'<span class="title">(.*?)</span>.*?'
r'<span class="other">(.*?)</span>.*?'-
r'<span class="rating_num".*?>(.*?)</s-an>.*?'
r'<span>(\d+人评价)</span>.*?'
r'<span class="inq">(.*?)</span>?',
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--- 练习5JSON统计 ---")
with open("movies.json", "r", encoding="utf-8") as f:
data = json.load(f)