import requests from bs4 import BeautifulSoup import time url = f'https://picsum.photos/' headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36'} response = requests.get(url,headers=headers,timeout=10) response.encoding = 'utf-8' soup = BeautifulSoup(response.text,'html.parser') for i in range(5): print(f"正在下载第{i+1}张图片...") try: img_response = requests.get(url,headers=headers,timeout=10) with open('image_{i}.jpg','wb') as f: f.write(img_response.content) print(f"第{i+1}张图片下载完成!") except Exception as e: print(f"第{i+1}张图片下载失败:{e}") time.sleep(1) print("5张图片全部下载完毕!")