From 00846b7015b8c49cc3cd2dc486e6eb55c63bac40 Mon Sep 17 00:00:00 2001 From: 2509165015 <2509165015@student.edu.cn> Date: Thu, 26 Mar 2026 16:00:23 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BD=91=E7=BB=9C=E6=95=B0=E6=8D=AE=E9=87=87?= =?UTF-8?q?=E9=9B=86=EF=BC=88=E7=88=AC=E8=99=AB=E5=9F=BA=E7=A1=80=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 0326+2509165015.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 0326+2509165015.py diff --git a/0326+2509165015.py b/0326+2509165015.py new file mode 100644 index 0000000..28d9115 --- /dev/null +++ b/0326+2509165015.py @@ -0,0 +1,30 @@ +import requests +from bs4 import BeautifulSoup +import os +if not os.path.exists('images'): + os.makedirs('images') +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' +} +url = 'https://picsum.photos/' +try: + response = requests.get(url, headers=headers, timeout=10) + response.raise_for_status() + soup = BeautifulSoup(response.text, 'html.parser') + img_tags = soup.find_all('img', class_='resize') + for index, img in enumerate(img_tags[:5]): + img_url = img.get('src') + if img_url: + if not img_url.startswith('http'): + img_url = f'https://picsum.photos{img_url}' + print(f"正在下载第 {index+1} 张图片: {img_url}") + img_response = requests.get(img_url, headers=headers, timeout=10) + img_response.raise_for_status() + img_name = f'images/image_{index+1}.jpg' + with open(img_name, 'wb') as f: + f.write(img_response.content) + print(f"✅ 保存成功: {img_name}") +except requests.exceptions.RequestException as e: + print(f"网络请求出错: {e}") +except Exception as e: + print(f"发生未知错误: {e}") \ No newline at end of file