python3+requests:get/post請求


1.get請求

(1)沒有請求參數類型

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

(2)有請求參數的類型(鍵值對形式表示參數)

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

(3)有請求頭(鍵值對形式表示請求頭)

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

2.post請求

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

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

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

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

(3)請求正文是raw

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

或者

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

(4)請求正文是binary

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

 轉載請注明出處:https://www.cnblogs.com/shapeL/p/9037035.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM