修改前的代碼
但是總顯示“失敗”
原因是
修改之后的代碼
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}")