SanYeCao-Nonebot/src/clover_image/download_image.py

21 lines
647 B
Python
Raw Normal View History

import aiohttp
2025-02-20 09:33:58 +00:00
async def download_image(url,file_path):
2025-02-20 09:33:58 +00:00
"""
下载图片
:param url:
:param file_path:
:return:
"""
try:
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
response.raise_for_status()
with open(file_path, 'wb') as file:
while True:
chunk = await response.content.read(8192)
if not chunk:
break
file.write(chunk)
except aiohttp.ClientError as e:
2025-02-20 09:33:58 +00:00
print(f"下载图片时出错: {e}")