Requests模块之-2、发送post请求

上一篇博客简单描述了怎么发送get请求,那么这一篇简要描述怎么发送post请求,以及入参形式、封装等

1、发送post请求

以聚合数据中"历史上的今天"接口为例

import requests
url = 'http://api.juheapi.com/japi/toh'
data = {
    "key":"7486da7f50cd55e6774fb3311b526d**",
    "v":'1.0',
    "month":12,
    "day":15
}
# 使用post发送请求时,大部分入参是以json形式传参,那么使用json=data即可
res = requests.post(url=url,data=data) 
print(res.json())

2、入参形式

2.1 parmas 传递查询字符串参数(get请求)

上一篇博客中有提到发送get请求,使用parmas传递参数

2.2 json类型的参数:参数类型为:Content-Type:application/json

res=requests.post(url=url,json=data)

2.3 表单类型的参数:Content-Type:“application/x-www-from-urlencoded”

response=requests.post(url=url,data=data)

2.4 from-data参数(用于上传文件):content-type:multipart/form-data

# files为字典类型数据,上传的文件为键值对的形式,参数名作为键
# 参数值是一个元组,内容为以下格式(文件名,打开的文件流,文件类型)
# file_data = {"参数名":("文件名",open("1215post请求.py","rb"),"text/py")}
# 注意:除上传的文件,接口的其他参数不能放在files中
file_data = {
       "test":("1215post请求.py",open("1215post请求.py","rb"),"text/py")
}
response = requests.post(url=url,files=file_data)

3、请求头

发送请求很多时候都需要添加一个User-Agent的请求头,因为很多接口都做了反爬虫操作,确认请求是用户发送的,不是代码发送的,详细解释可以百度。

headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.129 Safari/537.36"
}
# 发送请求时也一并加上请求头
response = requests.get(url=url,headers=headers)
print(response.headers)

4、封装

使用cookie + session鉴权的请求类进行封装(后续博客会介绍cookie、session作用以及区别)

import requests
class SendRequest():
    def __init__(self):
        # 创建sesion对象,后面使用创建的对象发送请求,不需要使用request.get/post发送请求了
        self.session = requests.session()
        
    def sendrequest(self, url, method, headers=None, params=None, data=None, json=None, files=None):
        # 调用send方法时,防止请求类型传入大写报错,统一转换为小写
        method = method.lower() 
        if method == "get":
            response = self.session.get(url=url, params=params, headers=headers)
        elif method == "post":
            response = self.session.post(url=url, json=json, data=data, files=files, headers=headers)
        return response
        
 # 调用封装类发送聚合数据中"历史上的今天"接口
 if __name__ == '__main__':
    Send = SendRequest()
    url = 'http://api.juheapi.com/japi/toh'
    data = {
    "key":"7486da7f50cd55e6774fb3311b526d**",
    "v":'1.0',
    "month":12,
    "day":15
    }
    A = Send.send(url=url,method='post',data=data)
    print(A.json())

输出结果
在这里插入图片描述

  • 6
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
### 回答1: 在使用 Requests 模块发送 POST 请求时,你需要在调用 `requests.post()` 函数时传入几个参数: 1. `url`: 这是你要发送 POST 请求URL。 2. `data`: 这是你要发送的数据。它可以是一个字典,列表,元组或字符串。如果你要发送表单数据,你应该使用字典并使用 `requests.post()` 函数的 `data` 参数传递它。 3. `json`: 这是你要发送的 JSON 数据。你可以使用这个参数来发送 JSON 格式的数据。 4. `headers`: 这是你要发送的 HTTP 头。你可以使用字典来指定 HTTP 头。 5. `cookies`: 这是你要发送的 cookie。你可以使用字典来指定 cookie。 6. `auth`: 这是你的身份验证信息。你可以使用元组来指定用户名和密码。 7. `timeout`: 这是等待服务器响应的最长时间(以秒为单位)。 例如,下面是使用 Requests 模块发送 POST 请求的示例代码: ``` import requests url = 'http://httpbin.org/post' data = {'key': 'value'} headers = {'Content-Type': 'application/json'} response = requests.post(url, data=data, headers=headers) print(response.text) ``` 这个示例代码向 `http://httpbin.org/post` 发送了一个 POST 请求,并使用 `data` 参数传递了一个字典作为表单数据,使用 `headers` 参数指定了 HTTP 头。 ### 回答2: Requests 模块发送 Post 请求的参数可以通过不同的方式传递。 第一种方式是通过 data 参数传递,可以将参数以字典形式传递给 data 参数,例如: ``` import requests data = {'key1': 'value1', 'key2': 'value2'} response = requests.post(url, data=data) ``` 第二种方式是通过 json 参数传递,可以将参数以字典形式传递给 json 参数,例如: ``` import requests data = {'key1': 'value1', 'key2': 'value2'} response = requests.post(url, json=data) ``` 第三种方式是通过 headers 参数传递,可以将参数以字典形式传递给 headers 参数,例如: ``` import requests headers = {'Content-Type': 'application/json'} response = requests.post(url, headers=headers) ``` 第四种方式是通过 cookies 参数传递,可以将参数以字典形式传递给 cookies 参数,例如: ``` import requests cookies = {'key1': 'value1', 'key2': 'value2'} response = requests.post(url, cookies=cookies) ``` 以上是 Requests 模块发送 Post 请求的一些常见参数传递方式,根据实际情况选择合适的方式进行参数传递。 ### 回答3: Requests 模块发送 post 请求时的参数可以通过 data 或者 json 参数来设置。 使用 data 参数时,可以传递一个字典类型的对象作为参数,其中包含了需要发送的数据。这个字典对象会被自动编码为表单形式的请求数据,然后发送给服务器。可以使用如下方式设置 data 参数: ```python import requests payload = {'key1': 'value1', 'key2': 'value2'} response = requests.post(url, data=payload) ``` 使用 json 参数时,可以传递一个字典类型的对象作为参数,其中包含了需要发送的数据。这个字典对象会被自动编码为 JSON 格式的请求数据,然后发送给服务器。可以使用如下方式设置 json 参数: ```python import requests payload = {'key1': 'value1', 'key2': 'value2'} response = requests.post(url, json=payload) ``` data 参数适用于普通的表单数据提交,而 json 参数适用于发送 JSON 数据的情况。根据实际需要选择合适的方式来设置参数。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值