From 3a9ef7477e663d497b1e3c180e0be7325176f9d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B0=A2=E5=98=89=E6=80=A1?= <2509165002@student.example.com> Date: Sat, 4 Jul 2026 15:26:41 +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 --- python.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ q1_1.py.url | 2 ++ q2_2.py.url | 2 ++ 3 files changed, 49 insertions(+) create mode 100644 python.py create mode 100644 q1_1.py.url create mode 100644 q2_2.py.url diff --git a/python.py b/python.py new file mode 100644 index 0000000..fe4fb2b --- /dev/null +++ b/python.py @@ -0,0 +1,45 @@ +import requests +from bs4 import BeautifulSoup +import time +import json # 补上json + # 请求头,模拟浏览器,防止被拦截 +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" + } +movie_list = [] + # 只爬前两页,每页25条,合计50 +for page in range(0, 50, 25): + url = f"https://movie.douban.com/top250?start={page}" + res = requests.get(url, headers=headers) + res.encoding = "utf-8" + soup = BeautifulSoup(res.text, "html.parser") + items = soup.find_all("div", class_="item") + +for item in items: + # 电影名称 + title = item.find("span", class_="title").get_text(strip=True) + # 主演信息 +info_text = item.find("div", class_="bd").p.get_text(strip=True) + # 截取导演演员部分 +actor_info = info_text.split("\n")[0] + + # 短评,部分电影无短评做容错 +quote_tag = item.find("span", class_="inq") +short_comment = quote_tag.get_text(strip=True) if quote_tag else "无短评" + +data = { + "电影名": title, + "主创主演": actor_info, + "经典短评": short_comment + } +movie_list.append(data) +print(data) + # 每页延时1秒,降低访问频率,避免封IP +time.sleep(1) + +# 打印总数量 +print(f"\n一共抓取{len(movie_list)}部电影") + +with open("movies.json","w",encoding="utf-8") as f: + json.dump(movie_list, f, ensure_ascii=False, indent=2) +print("数据已保存到 movies.json 文件!") \ No newline at end of file diff --git a/q1_1.py.url b/q1_1.py.url new file mode 100644 index 0000000..a7e9484 --- /dev/null +++ b/q1_1.py.url @@ -0,0 +1,2 @@ +[InternetShortcut] +URL=https://gitea.detr.top/2509165035/simulated-examination/commit/6ea7496ca845856c5b827402294afa2fe84bae84#diff-63b9715bde72e56b32640d22cfb836ec12fe1b5d diff --git a/q2_2.py.url b/q2_2.py.url new file mode 100644 index 0000000..ce47a90 --- /dev/null +++ b/q2_2.py.url @@ -0,0 +1,2 @@ +[InternetShortcut] +URL=https://gitea.detr.top/2509165035/simulated-examination/commit/6ea7496ca845856c5b827402294afa2fe84bae84#diff-5b41195ecb85f5de622ae8983fbdf7a8edc48c82