网络数据采集(爬虫基础)

This commit is contained in:
2509165015
2026-03-26 16:00:23 +08:00
parent d17df9bc78
commit 00846b7015

30
0326+2509165015.py Normal file
View File

@@ -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}")