Created
June 5, 2021 16:13
-
-
Save santoshpy/b275c64a4a19342b2a5d0e07a3e47921 to your computer and use it in GitHub Desktop.
Python3 Download Image Asynchronously From List of URLs.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
To Download Image Asynchronously From List of URLs | |
""" | |
import asyncio | |
from pathlib import Path | |
import aiofiles | |
from aiohttp import ClientSession | |
# List of image urls to download | |
URLS = [] | |
FILE_FORMAT = "jpg" | |
# Local download path, Create a directory if not exist | |
DOWNLOAD_DIR = Path("./Images") | |
DOWNLOAD_DIR.mkdir(parents=True, exist_ok=True) | |
async def download(session, url, index): | |
async with session.get(url) as response: | |
filename = DOWNLOAD_DIR / f"{index}.{FILE_FORMAT}" | |
img = await response.content.read() | |
async with aiofiles.open(filename, "wb") as img_file: | |
await img_file.write(img) | |
async def main(): | |
headers = {"Connection": "close"} | |
async with ClientSession(headers=headers) as session: | |
tasks = [ | |
download(session, url, index) for index, url in enumerate(URLS, start=1) | |
] | |
await asyncio.gather(*tasks) | |
if __name__ == "__main__": | |
asyncio.run(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment