# -*-coding:utf-8-*-
# Author:Lai
import aiohttp, aiofiles, asyncio
# 下载器
async def fetch(session, url):
async with session.get(url) as resp:
return await resp.read()
# 单个任务
async def main1():
async with aiohttp.ClientSession() as session:
url = "https://www.xxx.com"
res = await fetch(session, url)
print(res.decode("GBK"))
# 多个任务
async def main2():
async with aiohttp.ClientSession() as session:
urls = ["https://www.xxx1.com","https://www.xxx2.com"]
tasks = [asyncio.create_task(fetch(session, url)) for url in urls]
b_list = await asyncio.gather(*tasks)
if __name__ == "__main__":
asyncio.run(main1())
# asyncio.run(main1())
原文:https://www.cnblogs.com/lilied/p/13583515.html