Python发送Post请求及解析响应结果

一、Post请求

1、使用python发送一个Post请求

有时候遇到请求url中有很多参数。

1.1 示例1

accounts和pwd请到http://shop-xo.hctestedu.com/注册。

import requests

# 请求体
data = {
   
    "accounts": "xx",
    "pwd": "xxx",
    "type": "username"
}

# 只有php项目需要application和application_client_type
# 写法一, 在请求Url中带上所有参数,application和application_client_type,用&隔开
response = requests.post(url="http://shop-xo.hctestedu.com/index.php?"
                                "s=api/user/login"
                                "&application=app"
                                "&applicaiton_client_type=weixin", json=data)

# 输出响应结果
print(response.text)

执行结果:

{"msg":"登录成功","code":0,"data":{"id":"7073","username":"xx","nickname":"","mobile":"","email":"","avatar":"http:\/\/shop-xo.hctestedu.com\/static\/index\/default\/images\/default-user-avatar.jpg","alipay_openid":"","weixin_openid":"","weixin_unionid":"","weixin_web_openid":"","baidu_openid":"","toutiao_openid":"","qq_openid":"","qq_unionid":"","integral":"189","locking_integral":"0","referrer":"0","add_time":"1646195490","add_time_text":"2022-03-02 12:31:30","mobile_security":"","email_security":"","user_name_view":"xx","is_mandatory_bind_mobile":0,"token":"xxxxxx"}}

1.2 示例2

使用不定长参数 params,将url中需要的参数单独封装。

import requests

# 请求体
data = {
   
    "accounts": "xx",
    "pwd": "xxx",
    "type": "username"
}

# 使用不定长参数params
param_data = {
   
    "application": "app",
    "application_client_type": "weixin"
}
# 写法2:使用不定长参数params
response = requests.post(url="http://shop-xo.hctestedu.com/index.php?"
                             "s=api/user/login", params=param_data, json=data)

# 输出响应结果
print(response.text)

执行结果:

{"msg":"登录成功","code":0,"data":{"id":"22299","username":"xx","nickname":"","mobile":"","email":"","avatar":"http:\/\/shop-xo.hctestedu.com\/static\/index\/default\/images\/default-user-avatar.jpg","alipay_openid":"","weixin_openid":"","weixin_unionid":"","weixin_web_openid":"","baidu_openid":"","toutiao_openid":"","qq_openid":"","qq_unionid":"","integral":"0","locking_integral":"0","referrer":"0","add_time":"1678005098","add_time_text":"2023-03-05 16:31:38","mobile_security":"","email_security":"","user_name_view":"xx","is_mandatory_bind_mobile":0,"token":"xxxxx"}}

二、获取Response body

1、response.text

用type()查看response.text的类型,是str

import requests

# 请求体
data = {
   
    "accounts": "xx",
    "pwd": "xxx",
    "type": "username"
}

# 使用不定长参数params
param_data = {
   
    "application": "app",
    "application_client_type": "weixin"
}
# 写法2:使用不定长参数params
response = requests.post(url="http://shop-xo.hctestedu.com/index.php?"
                             "s=api/user/login", params=param_data, json=data)

# 输出响应结果
print(response.text)
print(type(response.text))

执行结果:

{"msg":"登录成功","code":0,"data":{"id":"22299","username":"xx","nickname":"","mobile":"","email":"","avatar":"http:\/\/shop-xo.hctestedu.com\/static\/index\/default\/images\/default-user-avatar.jpg","alipay_openid":"","weixin_openid":"","weixin_unionid":"","weixin_web_openid":"","baidu_openid":"","toutiao_openid":"","qq_openid":"","qq_unionid":"","integral":"0","locking_integral":"0","referrer":"0","add_time":"1678005098","add_time_text":"2023-03-05 16:31:38","mobile_security":"","email_security":"","user_name_view":"xx","is_mandatory_bind_mobile":0,"token":"xxxxx"}}
<class 'str'>

2、response.json()

用type()查看response.json()的类型,是dict

import requests

# 请求体
data = {
   
    "accounts": "xx",
    "pwd": "xxx",
    "type": "username"
}

# 使用不定长参数params
param_data = {
   
    "application": "app",
    "application_client_type": "weixin"
}
# 写法2:使用不定长参数params
response = requests.post(url="http://shop-xo.hctestedu.com/index.php?"
                             "s=api/user/login", params=param_data, json=data)

# 输出响应结果
print(response.json())
print(type(response.json()))

执行结果:

{"msg":"登录成功","code":0,"data":{"id":"22299","username":"xx","nickname":"",
### 回答1: 我可以给你一些有关Python发送POST请求的信息,可以使用Python的标准库urllib和requests包。使用urllib,您可以使用urllib.request.urlopen()函数和urllib.parse模块来发送POST请求。如果您使用requests库,您可以使用requests.post()函数来发送POST请求。 ### 回答2: Python发送POST请求可以使用`requests`库中的`post()`方法。 首先,需要安装`requests`库。可以通过以下命令来安装: ``` pip install requests ``` 接下来,可以使用以下代码来发送POST请求: ```python import requests # 请求的URL url = 'http://example.com/post' # POST请求的参数 data = { 'key1': 'value1', 'key2': 'value2' } # 发送POST请求 response = requests.post(url, data=data) # 获取响应内容 content = response.text # 输出响应内容 print(content) ``` 上述代码中,我们首先导入`requests`库。设置了请求的URL和POST请求的参数。然后使用`post()`方法发送POST请求,并将返回的响应保存在`response`变量中。最后,我们可以通过`response.text`来获取响应的内容,并将其打印输出。 这就是使用Python发送POST请求的基本步骤。根据实际情况,你可能需要进一步处理请求响应,例如对返回的数据进行解析和处理。 ### 回答3: Python发送POST请求可以通过使用requests库来实现。具体步骤如下: 1. 导入requests库 ```python import requests ``` 2. 创建一个字典对象来存储请求的参数 ```python data = { 'key1': 'value1', 'key2': 'value2' } ``` 3. 调用requests库中的post方法,并将请求的URL和参数传递给post方法的对应参数 ```python response = requests.post(url, data=data) ``` 其中,`url`表示请求的URL地址。 4. 获取响应结果 ```python response_text = response.text ``` 5. 解析和处理响应结果 ```python # 如果响应结果是JSON格式的数据 response_json = response.json() # 获取响应的状态码 status_code = response.status_code # 获取响应头信息 headers = response.headers ``` 6. 处理错误和异常 ```python try: response.raise_for_status() except Exception as e: print('请求错误: ', e) ``` 综上所述,以上是使用Python发送POST请求的基本步骤。根据实际需求,可以适当调整和增加代码。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Summer@123

不积跬步无以至千里,感谢支持!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值