From 5b0a43f69aaf1d5a385aac2ba99a844d041f2cd6 Mon Sep 17 00:00:00 2001 From: ayushk780 <40500298+ayushk780@users.noreply.github.com> Date: Fri, 16 Oct 2020 19:58:10 +0530 Subject: [PATCH] Added Asynchronous python code Asynchronous python code for web scraping --- Asyncio/Image-downloader/downloader.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 Asyncio/Image-downloader/downloader.py diff --git a/Asyncio/Image-downloader/downloader.py b/Asyncio/Image-downloader/downloader.py new file mode 100644 index 0000000..c9012d1 --- /dev/null +++ b/Asyncio/Image-downloader/downloader.py @@ -0,0 +1,26 @@ +# Download waifu images from www.thiswaifudoesnotexist.net using asyncio + +import random +import io +import requests +import aiohttp +import asyncio +from PIL import Image + + +async def async_fetch(url: str) -> bytes: + """fetch html content of a page. Returns a bytestring of the content""" + async with aiohttp.ClientSession() as session: + async with session.get(url) as response: + html_in_bytestring = await response.content.read() + return html_in_bytestring + +async def waifu_fun(): + rnum = random.randint(0, 100000) + url = 'https://www.thiswaifudoesnotexist.net/example-' + str(rnum) + '.jpg' + html = await async_fetch(url) + img = Image.open(io.BytesIO(html)) + img.save(f'{rnum}.jpg') + print("Image Saved") + +asyncio.run(waifu_fun())