zl程序教程

您现在的位置是:首页 >  后端

当前栏目

python3+requests:get、post请求(python get、post)

PythonPython3 请求 get post requests
2023-09-11 14:14:47 时间

1.get请求

(1)没有请求参数类型

response = requests.get(url='')
print(response.text)

(2)有请求参数的类型(键值对形式表示参数)

response = requests.get(url='',params={'key1':'value1','key2':'value2'})
print(response.text)

(3)有请求头(键值对形式表示请求头)

response = requests.get(url='',headers={'key1':'value1'})
print(response.text)

2.post请求

(1)请求正文是application/x-www-form-urlencoded

res = requests.post(url='',data={'key1':'value1','key2':'value2'},headers={'Content-Type':'application/x-www-form-urlencoded'})
print (res.json)
print (res.text)

(2)请求正文是multipart/form-data

res = requests.post(url='',data={'key1':'value1','key2':'value2'},headers={'Content-Type':'multipart/form-data'})
print (res.json)
print (res.text)

(3)请求正文是raw

传入xml格式文本
res = requests.post(url='',data='<?xml  ?>',headers={'Content-Type':'text/xml'})
print (res.json)
print (res.text)
传入json格式文本
res = requests.post(url='',data=json.dumps({'key1':'value1','key2':'value2'}),headers={'Content-Type':'application/json'})
print (res.json)
print (res.text)

或者

res = requests.post(url='',json={{'key1':'value1','key2':'value2'}},headers={'Content-Type':'application/json'})
print (res.json)
print (res.text)

(4)请求正文是binary

res = requests.post(url='',files={'file':open('test.xls','rb')},headers={'Content-Type':'binary'})
print (res.json)
print (res.text)