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请求上传文件

你可能感兴趣的:(python技巧,python,开发语言,json)