Python文件操作完全指南

This commit is contained in:
2509165020
2026-03-31 11:29:26 +08:00
parent eb5d1af1e1
commit d8635023a1
6 changed files with 103 additions and 0 deletions

View File

@@ -0,0 +1,33 @@
import requests
import re
import json
url = "https://movie.douban.com/top250"
headers = {"User-Agent": "Mozilla/5.0"}
resp = requests.get(url, headers=headers)
html = resp.text
pattern = re.compile(
r'<em class="">(\d+)</em>.*?'
r'<span class="title">(.*?)</span>.*?'
r'<span class="other">(.*?)</span>.*?'
r'<span class="rating_num">(.*?)</span>.*?'
r'<span class="inq">(.*?)</span>?',
re.S
)
movies = pattern.findall(html)[:10]
result = []
for m in movies:
result.append({
"rank": int(m[0]),
"title": m[1],
"en_title": m[2].replace("/", "").strip(),
"rating": m[3],
"quote": m[4] if len(m) > 4 else ""
})
with open("movies.json", "w", encoding="utf-8") as f:
json.dump(result, f, ensure_ascii=False, indent=4)
print("已保存到 movies.json")