爬虫 -- requests、httpx、aiohttp速度对比

代码

  • 测试100个请求
# -*- coding: utf-8 -*-
# @Author   : markadc

import asyncio
import time
import requests
import aiohttp
import httpx

url = "https://www.baidu.com"


def use_requests():
    begin = time.time()
    session = requests.Session()
    for _ in range(100):
        session.get(url)
    print(time.time() - begin, "use_requests")


async def a_make_requests(url, client):
    async with client.get(url) as resp:
        await resp.text()


async def use_aiohttp():
    begin = time.time()
    async with aiohttp.ClientSession() as cli:
        tasks = [asyncio.create_task(a_make_requests(url, cli)) for _ in range(100)]
        await asyncio.wait(tasks)
    print(time.time() - begin, "use_aiohttp")


async def use_httpx():
    begin = time.time()
    async with httpx.AsyncClient() as cli:
        tasks = [asyncio.create_task(cli.get(url)) for _ in range(100)]
        await asyncio.wait(tasks)
    print(time.time() - begin, "use_httpx")


if __name__ == '__main__':
    use_requests()
    asyncio.get_event_loop().run_until_complete(use_aiohttp())
    asyncio.get_event_loop().run_until_complete(use_httpx())

结果

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值