1. 程式人生 > >python3+requests:get/post請求

python3+requests:get/post請求

val value size content response json格式 dump AR 請求參數

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})

python3+requests:get/post請求