From 5c576c811f47a2a595fdc24e41e3943401c9e60b Mon Sep 17 00:00:00 2001 From: Murasame Noa <162931251+LeiSureLyYrsc@users.noreply.github.com> Date: Tue, 26 Aug 2025 01:40:10 +0800 Subject: [PATCH] Create BackgroundsGet.py --- BackgroundsGet.py | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 BackgroundsGet.py diff --git a/BackgroundsGet.py b/BackgroundsGet.py new file mode 100644 index 0000000..46ab7b4 --- /dev/null +++ b/BackgroundsGet.py @@ -0,0 +1,43 @@ +import httpx +import os + +def download_image_with_httpx_auto_redirect(url, save_path): + """ + 使用httpx库自动处理重定向下载图片 + + Args: + url (str): 图片URL + save_path (str): 保存路径 + """ + try: + # httpx默认也会自动跟随重定向 + with httpx.Client(follow_redirects=True) as client: + response = client.get(url, timeout=30.0) + + # 检查状态码 + if response.status_code == 200: + # 确保保存目录存在 + os.makedirs(os.path.dirname(save_path), exist_ok=True) + + # 写入文件 + with open(save_path, 'wb') as f: + f.write(response.content) + + Background = response.content + + print(f"图片已成功保存到: {save_path}") + print(f"最终URL: {response.url}") # 显示最终重定向后的URL + return Background + else: + print(f"请求失败,状态码: {response.status_code}") + return False + + except Exception as e: + print(f"下载失败: {e}") + return False + +# 使用示例 +if __name__ == "__main__": + image_url = "https://www.loliapi.com/acg/" + save_path = "images/downloaded_image.jpg" + download_image_with_httpx_auto_redirect(image_url, save_path) \ No newline at end of file