python3+requests:get/post请求

时间:2024-05-19 20:35:38

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

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

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

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

(3)请求正文是raw

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

或者

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

(4)请求正文是binary

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

转载请注明出处:https://www.cnblogs.com/shapeL/p/9037035.html