修改前的代码
但是总显示“失败”
原因是
修改之后的代码
import requests
import os
from urllib.parse import unquote# 原始URL
url = 'https://cn.bing.com/images/search?view=detailV2&ccid=TnImuvQ0&id=5AE65CE4BE05EE7A79A73EEFA37578E87AE19421&thid=OIP.TnImuvQ0eOu3Ncn8G7W4BQHaE8&mediaurl=https%3a%2f%2fpic.nximg.cn%2ffile%2f20230512%2f33688781_144207815103_2.jpg&exph=683&expw=1024&q=%e9%87%8d%e5%ba%86%e9%82%ae%e7%94%b5%e5%a4%a7%e5%ad%a6%e5%9b%be%e7%89%87%e5%ba%93&simid=608017321150603477&FORM=IRPRST&ck=82C8DD3330C84300350495177BFC73F6&selectedIndex=0&itb=0&idpp=overlayview&ajaxhist=0&ajaxserp=0'# 解析出实际的图片URL
media_url_encoded = url.split('mediaurl=')[-1].split('&')[0]
media_url = unquote(media_url_encoded)# 定义保存图片的目录和文件名
root = 'E://'
filename = media_url.split('/')[-1]
path = os.path.join(root, filename)try:# 确保目录存在if not os.path.exists(root):os.makedirs(root)# 如果文件不存在,则下载图片if not os.path.exists(path):response = requests.get(media_url)response.raise_for_status() # 如果请求出错,这里会抛出HTTPError异常with open(path, "wb") as f:f.write(response.content)print("文件保存成功")else:print("文件已存在,未进行下载")
except requests.RequestException as e:print(f"请求错误: {e}")
except Exception as e:print(f"发生错误: {e}")