From 16102b329cd5ead44176f687265ddf6c31ed484d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=BF=9E=E5=85=B4=E6=9D=B0?= <2509165004@student.example.com> Date: Tue, 31 Mar 2026 11:28:24 +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 --- 260331-2509165004.py | Bin 0 -> 1024 bytes lxj.py | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 260331-2509165004.py create mode 100644 lxj.py diff --git a/260331-2509165004.py b/260331-2509165004.py new file mode 100644 index 0000000000000000000000000000000000000000..06d7405020018ddf3cacee90fd4af10487da3d20 GIT binary patch literal 1024 ScmZQz7zLvtFd70QH3R?z00031 literal 0 HcmV?d00001 diff --git a/lxj.py b/lxj.py new file mode 100644 index 0000000..8e0810d --- /dev/null +++ b/lxj.py @@ -0,0 +1,33 @@ +import requests +import re + +# 1. 发送请求获取页面 +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 + +# 2. 用正则表达式提取电影名称 +# 电影名称在 中 +pattern = r'([^<&]+)' +titles = re.findall(pattern, html) + +# 3. 过滤掉英文名(只保留中文名) +chinese_titles = [t for t in titles if not t.startswith('/')] + +# 取前10个 +top10 = chinese_titles[:10] + +# 4. 保存到文本文件 +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()) \ No newline at end of file