import requests
import asyncio
import aiohttp
async def download(url):
print("开始下载")
# 这里可以改成aiohttp.ClientSeiion().get()
async with aiohttp.ClientSeiion() as session:
async with session.get(url) as resp:
resp.content.read()#<==>resp.content
resp.text() #<==>resp.text
resp.json() #<==>resp.json()
with open("自己写名字",mode="wb") as f:
f.write(await resp.content.read()) #读取内容是异步的
await asyncio.sleep(3)
print("下载完成")
async def main():
urls = [
"https://www.baidu.com",
"https://www.bilibili.com",
"https://www.taobao.com"
]
tasks = []
for items in urls:
d = download(items)
tasks.append(d)
await asyncio.wait(tasks)
# python3.8以后需要这么写
# for items in urls:
# d = asyncio.create_task(download(items))
# tasks.append(d)
# await asyncio.wait(tasks)
if __name__ == '__main__':
asyncio.run(main())
过时警告
# python3.8以后需要这么写
for items in urls:
d = asyncio.create_task(download(items))
tasks.append(d)
await asyncio.wait(tasks)
tip:
写入文件也是IO操作,要将写入文件异步,需要学习aiofiles模块