import asyncio
from contextlib import asynccontextmanager
from aiohttp import ClientSession, CookieJar, TCPConnector
@asynccontextmanager
async def client_session():
async with ClientSession(
connector=TCPConnector(ssl=False),
cookie_jar=CookieJar(unsafe=True)
) as session:
yield session
async def main():
async with client_session() as session:
res = await session.get('https://www.baidu.com/')
print(res.text)
asyncio.run(main())
运行结果:
<bound method ClientResponse.text of <ClientResponse(https://www.baidu.com/) [200 OK]>
<CIMultiDictProxy('Cache-Control': 'no-cache', 'Connection': 'keep-alive', 'Content-Encoding': 'br', 'Content-Type': 'text/html', 'Date': 'Thu, 07 Aug 2025 08:15:32 GMT', 'P3p': 'CP=" OTI DSP COR IVA OUR IND COM "', 'P3p': 'CP=" OTI DSP COR IVA OUR IND COM "', 'Pragma': 'no-cache', 'Server': 'BWS/1.1', 'Set-Cookie': 'BAIDUID=3BAC59A4774E995E52EBF0F92B53F060:FG=1; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com', 'Set-Cookie': 'BIDUPSID=3BAC59A4774E995E52EBF0F92B53F060; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com', 'Set-Cookie': 'PSTM=1754554532; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com', 'Set-Cookie': 'BAIDUID=3BAC59A4774E995E3AE2847419C97563:FG=1; max-age=31536000; expires=Fri, 07-Aug-26 08:15:32 GMT; domain=.baidu.com; path=/; version=1; comment=bd', 'Traceid': '175455453206404628586749780845623367795', 'Vary': 'Accept-Encoding', 'X-Ua-Compatible': 'IE=Edge,chrome=1', 'X-Xss-Protection': '1;mode=block', 'Transfer-Encoding': 'chunked')>
>
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。