一、安装
httpx是Python新一代的网络请求库
httpx与 requests 库的基本使用方法几乎是一模一样的
基于Python3的功能齐全的http请求模块
既能发送同步请求,也能发送异步请求
支持HTTP/1.1和HTTP/2
支持直接向WSGI应用程序或者ASGI应用程序发送请求
pip install httpx
二、请求类型
import httpx
httpx.get('https://www.baidu.com')
httpx.post('https://www.baidu.com')
httpx.put('https://www.baidu.com')
httpx.delete('https://www.baidu.com')
httpx.head('https://www.baidu.com')
httpx.options('https://www.baidu.com')
三、带参数请求
import httpx
data = {
'name': 'autofelix',
'age': 25
}
response = httpx.get('https://www.baidu.com', params=data)
print(response.url)
print(response.text)
四、自定义 headers
import httpx
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko'
}
response =httpx.get('https://www.baidu.com', headers=headers)
print(response.text)
五、请求的属性
import httpx
response = httpx.get('https://www.baidu.com')
# 响应状态码
response.status_code
# 响应头
response.headers
# 响应cookie
response.cookies
# 请求url
response.url
# 历史记录
response.history
六、使用client发送请求
import httpx
with httpx.Client() as client:
response = client.get('https://www.baidu.com')
print(response.text)
七、HTTP代理
import httpx
proxies= {
'http': 'http://127.0.0.1:9999',
'https': 'http://127.0.0.1:8888'
}
with httpx.Client(proxies=proxies) as client:
response = client.get('https://www.baidu.com')
print(response)
八、超时处理
import httpx
# 普通请求超时处理
httpx.get('https://www.baidu.com', timeout=10.0)
# 普通请求关闭超时处理
httpx.get('https://www.baidu.com', timeout=None)
# client实例超时处理
with httpx.Client() as client:
client.get('https://www.baidu.com', timeout=None)
# client实例关闭超时处理
with httpx.Client() as client:
client.get('https://www.baidu.com', timeout=10.0)
九、SSL验证
import httpx
# 自带ca证书
response = httpx.get('https://www.baidu.com', verify="path/cert/client.pem")
# 完全禁用SSL验证
response = httpx.get('https://www.baidu.com', verify=False)
十、异步操作
使用async/await语句来进行异步操作
使用异步client比使用多线程发送请求更加高效,更能体现明显的性能优势
import asyncio
import httpx
async def main():
async with httpx.AsyncClient() as client:
response = await client.get('https://www.baidu.com')
print(response)
if __name__ == '__main__':
asyncio.run(main())
以上就是本次分享的全部内容,现在想要学习编程的小伙伴欢迎关注Python技术大本营,获取更多技能与教程。