Python异步编程——aiohttp 发起异步请求

关于aiohttp更详细的用法,请参见:aiohttp网址

先安装aiohttp: pip install aiohttp

并发请求的示例如下:

import asyncio
import aiohttp

urls = ["http://python.org",
        "http://blog.csdn.net/lianshaohua"
        ]

async def request(url: str):
    async with aiohttp.ClientSession() as session:
        async with session.get(url) as response:  # 发起请求并等待
            print(f"url:{url}")
            print(f"Status:{response.status}")
            print(f"Concent-type:{response.headers['content-type']}")
            body = await response.text()
            # 从 body 部分分析并抓取自己需要的数据

async def start_request():
    tasks = []
    for url in urls:
        task = asyncio.create_task(request(url))  # 同时发起多个 http 的请求
        tasks.append(task)

    for task in tasks:
        await task


if __name__ == "__main__":
    asyncio.get_event_loop().run_until_complete(start_request())

猜你喜欢

转载自blog.csdn.net/lianshaohua/article/details/112828684