Discord.py: Can't Figure Out How To Use Aiohttp In Place Of Requests For This Use Case
My original code (which made use of the requests library): sess = requests.session() req = sess.get(i) #i is a url soup = BeautifulSoup(req.content, 'html.parser') link = soup.find
Solution 1:
Note that I have included aiofile
to asynchronously write the file, as big files will cause the code to block.
import aiohttp
import asyncio
from bs4 import BeautifulSoup
import os
from aiofile import AIOFile
async def write_file():
sess = aiohttp.ClientSession()
req = await sess.get('https://www.gutenberg.org/ebooks/2600')
soup = BeautifulSoup(await req.read(), 'html.parser')
link = soup.find('a', text='EPUB (no images)')['href']
link = 'https://www.gutenberg.org' + link
req2 = await sess.get(link)
async with AIOFile(os.path.join('C:\\save\\location', 'war.epub'), 'wb') as x_file:
await x_file.write(await req2.read())
await sess.close()
loop = asyncio.get_event_loop()
loop.run_until_complete(write_file())
Post a Comment for "Discord.py: Can't Figure Out How To Use Aiohttp In Place Of Requests For This Use Case"