Files
2026-03-31 11:30:07 +08:00

26 lines
683 B
Python

import requests
import re
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
}
url = 'https://movie.douban.com/top250'
response = requests.get(url, headers=headers)
html = response.text
pattern = r'<span class="title">([^<&]+)</span>'
titles = re.findall(pattern, html)
chinese_titles = [t for t in titles if not t.startswith('/')]
top10 = chinese_titles[:10]
with open('movies.txt', 'w', encoding='utf-8') as f:
for i, title in enumerate(top10, 1):
f.write(f'{i}. {title}\n')
print('已保存前10部电影到 movies.txt')
with open('movies.txt', 'r', encoding='utf-8') as f:
print(f.read())