python学习day3
1.异步请求流程
import asyncio from aiohttp import ClientSession url = 'https://www.baidu.com' headers = { 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.87 Safari/537.36 SE 2.X MetaSr 1.0' } async def get_baidu(): # 创建客户端session async with ClientSession() as session: # 发送请求 async with session.get(url, headers=headers) as response: # 获取响应文本 res = await response.text() print(res) if __name__ == '__main__': asyncio.run(get_baidu())