From d8de74c4603f61047a43de2466ac8dca362f3fe7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E5=85=B4=E5=AE=87?= <2505155046@student.example.com> Date: Thu, 18 Jun 2026 16:08:27 +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 --- 260618 2505155046.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 260618 2505155046.py diff --git a/260618 2505155046.py b/260618 2505155046.py new file mode 100644 index 0000000..95243d0 --- /dev/null +++ b/260618 2505155046.py @@ -0,0 +1,42 @@ +import requests +from bs4 import BeautifulSoup +import json +import time + +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" +} + +movies = [] +for page in range(2): + url = f"https://movie.douban.com/top250?start={page*25}" + response = requests.get(url, headers=headers) + soup = BeautifulSoup(response.text, "html.parser") + + items = soup.find_all("div", class_="item") + for idx, item in enumerate(items): + + rank = page * 25 + idx + 1 + + title = item.find("span", class_="title").text.strip() + + info_text = item.find("div", class_="bd").p.text.strip() + actors_line = info_text.split("\n")[0] if "\n" in info_text else info_text + actors = actors_line.split("主演: ")[-1].strip() if "主演: " in actors_line else "未知主演" + + short_comment_tag = item.find("span", class_="inq") + short_comment = short_comment_tag.text.strip() if short_comment_tag else "暂无短评" + + movies.append({ + "rank": rank, + "title": title, + "actors": actors, + "short_comment": short_comment + }) + + time.sleep(1) + +with open("movies.json", "w", encoding="utf-8") as f: + json.dump(movies, f, ensure_ascii=False, indent=4) + +print("✅ 数据采集完成,已保存到 movies.json") \ No newline at end of file