首先需要导入aiohttp模块 import aiohttp 其次通过aiohttp.ClientSession()来代替requests async with aiohttp.ClientSession() as session: # requests async with session.get(url) as resp: # resp = requests.get() 执行完上述操作后
import aiohttp
其次通过aiohttp.ClientSession()来代替requests
async with aiohttp.ClientSession() as session: # requests
async with session.get(url) as resp: # resp = requests.get()
执行完上述操作后即可获得所请求的url的内容
使用异步操作时,需要注意的是与同步操作的细微差异
- 1.异步的await resp.content.read() <==>同步的resp.content()
- 2.异步的需要await挂起, resp.text() <==>同步的resp.text
注意:在执行异步操作的列表执行时需要用到await asyncio.wait(列表名称)