From 4bb3585e4f51c9f7633f91a5bcf7727236957004 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AD=99=E4=BC=A0=E6=95=8F?= <2509165031@student.example.com> Date: Tue, 24 Mar 2026 11:26:32 +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 --- 260324_2509165031.py | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 260324_2509165031.py diff --git a/260324_2509165031.py b/260324_2509165031.py new file mode 100644 index 0000000..7ddae63 --- /dev/null +++ b/260324_2509165031.py @@ -0,0 +1,39 @@ +import requests +from bs4 import BeautifulSoup + +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', + 'Referer': 'https://movie.douban.com/' +} + +all_movies = [] # 存放全部250部电影 + +# TOP250 共 10 页,每页 25 条 +for page in range(10): + start = page * 25 + url = f'https://movie.douban.com/top250?start={start}&filter=' + + try: + response = requests.get(url, headers=headers, timeout=15) + response.encoding = 'utf-8' + print(f'正在爬取第 {page+1}/10 页...') + + soup = BeautifulSoup(response.text, 'html.parser') + items = soup.find_all('div', class_='item') # 每部电影的父容器 + + for item in items: + # 提取电影名 + title_tag = item.find('span', class_='title') + if title_tag: + title = title_tag.get_text(strip=True) + if title not in all_movies: # 去重 + all_movies.append(title) + + except Exception as e: + print(f'第 {page+1} 页爬取异常:', e) + +# 输出结果 +print(f'\n✅ 成功爬取 {len(all_movies)} 部电影(TOP250 完整数据)') +print('='*30) +for idx, movie in enumerate(all_movies, 1): + print(f'{idx}. {movie}') \ No newline at end of file