import asyncio import aiohttp urls = [ "https://cdn.pixabay.com/photo/2021/11/16/08/13/window-6800397_960_720.jpg", "https://cdn.pixabay.com/photo/2021/01/29/11/33/game-5960731_960_720.jpg", "https://cdn.pixabay.com/photo/2021/11/13/12/19/cochem-castle-6791148_960_720.jpg" ] async def aiodownload(url): #async 异步 *** 作开头都得写 name = url.rsplit("/",1)[1] #设置文件的名字,resplit()是从后面开始截的 async with aiohttp.ClientSession() as session: #这里用with的原因是,with会有文件管理层自动帮我们执行close函数 async with session.get(url) as resp: with open(name, mode='wb') as f: f.write(await resp.content.read()) print(name,"t ") async def main(): tasks = [] for url in urls: #把链接写进url tasks.append(aiodownload(url)) #把返回的结果写进tasks await asyncio.wait(tasks) #把tasks传进异步协程 if __name__ == '__main__': asyncio.run(main())
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)