From 4d6d93cc141977f3e763af2305027b8b9d9717a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=83=A1=E7=BA=A2=E8=BE=89?= <2509165036@student.example.com> Date: Tue, 9 Jun 2026 10:29:13 +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 --- 1.py | 32 ++++++++++++++++++++++++++++++++ movies.csv | Bin 0 -> 1024 bytes 2 files changed, 32 insertions(+) create mode 100644 1.py create mode 100644 movies.csv diff --git a/1.py b/1.py new file mode 100644 index 0000000..d434889 --- /dev/null +++ b/1.py @@ -0,0 +1,32 @@ +import requests +from bs4 import BeautifulSoup +import csv +import time + +# 1. 发送请求 +url = 'https://movie.douban.com/top250' +headers = {'User-Agent': 'Mozilla/5.0...'} +response = requests.get(url, headers=headers) + +# 2. 解析数据 +soup = BeautifulSoup(response.text, 'lxml') +movies = [] + +for item in soup.select('.item'): + title = item.select_one('.title').get_text() + rating = item.select_one('.rating_num').get_text() + quote = item.select_one('.inq').get_text() if item.select_one('.inq') else '' + + movies.append({ + 'title': title.strip(), + 'rating': rating, + 'quote': quote + }) + +# 3. 保存为CSV +with open('movies.csv', 'w', newline='', encoding='utf-8') as f: + writer = csv.DictWriter(f, fieldnames=['title', 'rating', 'quote']) + writer.writeheader() + writer.writerows(movies) + +print(f'已保存 {len(movies)} 部电影到 movies.csv') diff --git a/movies.csv b/movies.csv new file mode 100644 index 0000000000000000000000000000000000000000..06d7405020018ddf3cacee90fd4af10487da3d20 GIT binary patch literal 1024 ScmZQz7zLvtFd70QH3R?z00031 literal 0 HcmV?d00001