一、定义
- post()方法将携带某些数据的POST请求发送到指定的URL
二、应用场景
- 提交表单所涉及到的增删改操作。
- 调用API,例如百度云的文字识别接口、阿里云的常用支付接口,都需要用POST请求。
- 发送/上传图片、音视频等文件资源。
三、使用方法
1)导入模块
import requests
2)封装数据
将要发送的数据封装到data中,封装形式可以是字典、json、元组等。
# 发送字典
post_dict = {'key1': 'value1', 'key2': 'value2'}
# 发送元组
post_tuple = (('key1', 'value1'), ('key1', 'value2'))
# 发送json
post_json = json.dumps({'some': 'data'})
r1 = requests.post("http://httpbin.org/post", data=post_dict)
r2 = requests.post("http://httpbin.org/post", data=post_tuple)
r3 = requests.post("http://httpbin.org/post", data=post_json)
3)定制header头和cookie信息
cookie = "token=;"
header = {
"cookie": cookie,
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate, br",