httpx
是一个现代化的HTTP
客户端库,支持同步和异步请求,功能比requests
更强大。
安装:
pip install httpx
同步请求
import httpx
response = httpx.get('https://api.example.com/data')
print(response.status_code)
print(response.json())
异步请求
import httpx
import asyncio
async def fetch_data():
async with httpx.AsyncClient() as client:
response = await client.get('https://api.example.com/data')
print(response.json())
asyncio.run(fetch_data())