From c5742e810360e5a4c4833d6cbce5e21b139c871a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BD=97=E6=AC=A3=E6=80=A1?= <2509165012@student.example.com> Date: Tue, 31 Mar 2026 11:26:23 +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.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 text.py diff --git a/text.py b/text.py new file mode 100644 index 0000000..f0c6e12 --- /dev/null +++ b/text.py @@ -0,0 +1,19 @@ +import requests +import re + +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/120.0.0.0 Safari/537.36" +} +response = requests.get(url, headers=headers) +html = response.text + +pattern = re.compile(r'(.*?)', re.S) +movie_names = pattern.findall(html) + +chinese_names = [name for name in movie_names if not name.startswith('/')] +top10_names = chinese_names[:10] +with open("movies.txt", "w", encoding="utf-8") as f: + for name in top10_names: + f.write(name + "\n") + print("练习1完成,已保存前10部电影名称到movies.txt") \ No newline at end of file