安装
pip install hpptx
基础使用
import httpx
url = ‘https://www.baidu.com‘
resp = httpx.get(url)
print(resp.status_code)
print(resp.headers)
print(resp.content.decode(‘utf-8‘))
异步请求方式一:
import httpx
import asyncio
url = ‘https://www.baidu.com‘
async def main():
async with httpx.AsyncClient() as client:
resp = await client.get(url)
print(resp)
asyncio.run(main())
异步请求方式二:需要升级 Python3.8才可以
httpx 还有很多强大的功能,使用方法跟 requests 差不多。
requests 能支持的 httpx 都支持,requests 不能干的事情 httpx 也能干。这个库野心还是很大的,当你的项目既要支持同步请求和也要支持异步请求的时候,httpx就该上场啦
原文:https://www.cnblogs.com/kai-/p/13377255.html