Files
2026-03-31 11:29:26 +08:00

31 lines
895 B
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import requests
import re
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36"
}
url = "https://movie.douban.com/top250"
try:
response = requests.get(url=url, headers=headers, timeout=10)
response.raise_for_status()
page_source = response.text
print("页面请求成功!")
except Exception as e:
print(f"请求失败:{e}")
exit()
pattern = re.compile(r'<span class="title">([^&]+?)</span>', re.S)
movie_names = pattern.findall(page_source)
target_names = movie_names[:10]
with open("movies.txt", "w", encoding="utf-8") as f:
for name in target_names:
f.write(name + "\n")
print(f"成功爬取{len(target_names)}部电影名已保存到movies.txt")
print("\n爬取结果预览:")
for i, name in enumerate(target_names, 1):
print(f"{i}. {name}")