From a5ceb6013fdc6d762db84d604ae923defde28c01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=B4=E5=AE=87=E6=99=A8?= <2509165026@student.example.com> Date: Thu, 18 Jun 2026 16:03:01 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20260326=202509165026.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 260326 2509165026.py | 68 +++++++++++++++++++++++++++----------------- 1 file changed, 42 insertions(+), 26 deletions(-) diff --git a/260326 2509165026.py b/260326 2509165026.py index 68f21e9..95243d0 100644 --- a/260326 2509165026.py +++ b/260326 2509165026.py @@ -1,26 +1,42 @@ -import requests -from bs4 import BeautifulSoup - - - -url = f'https://picsum.photos/' - -headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36'} - -response = requests.get(url, headers=headers,timeout=10) -response.encoding = 'utf-8' -soup = BeautifulSoup(response.text, 'html.parser') - -img_tags = soup.select('img.resize') -for i, img_tag in enumerate(img_tags[:5]): - img_src = img_tag.get('src') - print(f"正在下载第 {i+1} 张图片:{img_src}") - img_respnse = requests.get(img_src,headers=headers, timeout=10) - - - img_response = requests.get(img_src, timeout=10) - - with open('image.jpg', 'wb') as f: - f.write(img_response.content) - print("下载完成") - \ No newline at end of file +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