python 使用requests发送POST请求

简介: python 使用requests发送POST请求

一、定义

  • post()方法将携带某些数据的POST请求发送到指定的URL

二、应用场景

  1. 提交表单所涉及到的增删改操作。
  2. 调用API,例如百度云的文字识别接口、阿里云的常用支付接口,都需要用POST请求。
  3. 发送/上传图片、音视频等文件资源。

三、使用方法

1)导入模块

importrequests

2)封装数据

将要发送的数据封装到data中,封装形式可以是字典、json、元组等。

# 发送字典post_dict= {'key1': 'value1', 'key2': 'value2'}
# 发送元组post_tuple= (('key1', 'value1'), ('key1', 'value2'))
# 发送jsonpost_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=code_space;"header= {
"cookie": cookie,
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate, br",
"Accept-Language": "zh-CN,zh;q=0.9",
"Connection": "keep-alive",
"Content-Type": "application/json",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) ""AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36"}

四、测试demo

# -*- coding: utf-8 -*-"""@Time : 2022/1/18 11:36@Auth : 技术空间@File :post_demo.py@IDE :PyCharm@Motto:技术总是要日积月累的"""importrequestsimportjsonif__name__=='__main__':
cookie="token=code_space;"header= {
"cookie": cookie,
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate, br",
"Accept-Language": "zh-CN,zh;q=0.9",
"Connection": "keep-alive",
"Content-Type": "application/json",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36"    }
# 发送字典post_dict= {'key1': 'value1', 'key2': 'value2'}
# 发送元组post_tuple= (('key1', 'value1'), ('key1', 'value2'))
# 发送jsonpost_json=json.dumps({'some': 'data'})
r1=requests.post("http://httpbin.org/post", data=post_dict, headers=header, cookie=cookie)
r2=requests.post("http://httpbin.org/post", data=post_tuple, headers=header, cookie=cookie)
r3=requests.post("http://httpbin.org/post", data=post_json, headers=header, cookie=cookie)
print("r1返回的内容为-->"+r1.text)
print("r2返回的内容为-->"+r2.text)
print("r3返回的内容为-->"+r3.text)
相关文章
|
2天前
|
数据采集 存储 API
Python 网络请求:深入理解Requests库
Python 网络请求:深入理解Requests库
58 0
|
2天前
|
JSON 数据格式 Python
Python 的 requests 库是一个强大的 HTTP 客户端库,用于发送各种类型的 HTTP 请求
【5月更文挑战第9天】`requests` 库是 Python 中用于HTTP请求的强大工具。要开始使用,需通过 `pip install requests` 进行安装。发送GET请求可使用 `requests.get(url)`,而POST请求则需结合 `json.dumps(data)` 以JSON格式发送数据。PUT和DELETE请求类似,分别调用 `requests.put()` 和 `requests.delete()`。
28 2
|
2天前
|
JSON 数据格式 开发者
pip和requests在Python编程中各自扮演着不同的角色
【5月更文挑战第9天】`pip`是Python的包管理器,用于安装、升级和管理PyPI上的包;`requests`是一个HTTP库,简化了HTTP通信,支持各种HTTP请求类型及数据交互。两者在Python环境中分别负责包管理和网络请求。
31 5
|
2天前
|
数据采集 JSON API
如何用Python Requests发送请求
如何用Python Requests发送请求
10 0
|
2天前
|
API UED Python
使用Python进行异步HTTP请求的实践指南
使用Python进行异步HTTP请求的实践指南
20 4
|
2天前
|
安全 网络安全 Python
使用 Python 代码实现 ICMP Timestamp 请求和回应
使用 Python 代码实现 ICMP Timestamp 请求和回应
|
2天前
|
JSON 测试技术 API
Python的Api自动化测试使用HTTP客户端库发送请求
【4月更文挑战第18天】在Python中进行HTTP请求和API自动化测试有多个库可选:1) `requests`是最流行的选择,支持多种请求方法和内置JSON解析;2) `http.client`是标准库的一部分,适合需要低级别控制的用户;3) `urllib`提供URL操作,适用于复杂请求;4) `httpx`拥有类似`requests`的API,提供现代特性和异步支持。根据具体需求选择,如多数情况`requests`已足够。
15 3
|
2天前
|
存储 Python
Python网络数据抓取(3):Requests
Python网络数据抓取(3):Requests
16 5
|
1天前
|
网络协议 Unix Python
Python编程-----网络通信
Python编程-----网络通信
8 1