#reuqests.get() 同步的代码 ->异步操作aiohttp #pip install aiohttp import asyncio import aiohttp urls = {'网址'} async def aiodownload(url): name = url.rsplit("/",1)[1] #aiohttp.ClientSession() == reuqests async with aiohttp.ClientSession()as session: #requests async with session.get(url)as resp:#resp =requests.get() with open (name,'wb')as f:#创建文件 ##resp.content.read() f.write(await resp.content.read())#读取内容识异步的,需要await挂起 print(name) async def main(): tasks = [] for url in urls: tasks.append(aiodownload(url)) await asyncio.wait(tasks)#resp.content.read()== resp.content #resp.text() == resp.text #resp.json()=resp.json()
异步http请求aiohttp模块演示
最新推荐文章于 2024-06-14 23:48:57 发布