Python的requests的Post请求的N种方法
方式一:
content-type: form-data
(图片来源网络,侵删)
import requests url = 'https://baidu.com/' headers = { "user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36" } data = {"key1":"value1","key2":"value2"} res = requests.post(url=url,headers=headers,data=data) print(res.text)
方式二:
content-type: application/json;charset=utf-8
import requests import json url = 'https://baidu.com/' headers = { 'content-type': 'application/json;charset=utf-8' } data = {"key1":"value1","key2":"value2"} res = requests.post(url=url,headers=headers,data=json.dumps(data)) print(res.text)
方式三:
content-type: application/x-www-form-urlencoded
import requests from urllib.parse import urlencode headers = { 'content-type':'application/x-www-form-urlencoded', } data = { "key":"value" } url = 'https://www.xxxxx.com' res = requests.post(url=url,headers=headers,data=urlencode(data)) print(res.text)
方式四:
content-type: multipart/form-data; boundary=xxx
传送门:用python实现multipart/form-data; boundary=的post请求上传文件
文章版权声明:除非注明,否则均为主机测评原创文章,转载或复制请以超链接形式并注明出处。