python爬蟲使用requests發(fā)送post請(qǐng)求示例詳解
簡(jiǎn)介
HTTP協(xié)議規(guī)定post提交的數(shù)據(jù)必須放在消息主體中,但是協(xié)議并沒有規(guī)定必須使用什么編碼方式。服務(wù)端通過是根據(jù)請(qǐng)求頭中的Content-Type字段來獲知請(qǐng)求中的消息主體是用何種方式進(jìn)行編碼,再對(duì)消息主體進(jìn)行解析。具體的編碼方式包括:
application/x-www-form-urlencoded 最常見post提交數(shù)據(jù)的方式,以form表單形式提交數(shù)據(jù)。application/json 以json串提交數(shù)據(jù)。multipart/form-data 一般使用來上傳文件。
一、 以form表單發(fā)送post請(qǐng)求
Reqeusts支持以form表單形式發(fā)送post請(qǐng)求,只需要將請(qǐng)求的參數(shù)構(gòu)造成一個(gè)字典,然后傳給requests.post()的data參數(shù)即可。例:
# -*- coding: utf-8 -*-# author:Garyimport requestsurl = ’http://httpbin.org/post’ # 一個(gè)測(cè)試網(wǎng)站的urldata = {’key1’: ’value1’, ’key2’: ’value2’} # 你發(fā)送給這個(gè)的數(shù)據(jù)r = requests.post(url, data=data) # 使用requests的post方法,data接受你想發(fā)送的數(shù)據(jù)print(r.text) # 查看返回的內(nèi)容
輸出
{ “args”: {}, “data”: “”, “files”: {}, #你提交的表單數(shù)據(jù)“form”: { “key1”: “value1”,“key2”: “value2” }, “headers”: { …… “Content-Type”: “application/x-www-form-urlencoded”, …… }, “json”: null, …… }
可以看到,請(qǐng)求頭中的Content-Type字段已設(shè)置為application/x-www-form-urlencoded,且data = {‘key1’: ‘value1’, ‘key2’: ‘value2’}以form表單的形式提交到服務(wù)端,服務(wù)端返回的form字段即是提交的數(shù)據(jù)。
二、 以json形式發(fā)送post請(qǐng)求
可以將一json串傳給requests.post()的data參數(shù),
# -*- coding: utf-8 -*-# author:Garyimport requestsimport jsonurl = ’http://httpbin.org/post’ # 一個(gè)測(cè)試網(wǎng)站的urljson_data = json.dumps({’key1’: ’value1’, ’key2’: ’value2’}) # 你發(fā)送給這個(gè)的數(shù)據(jù),數(shù)據(jù)格式轉(zhuǎn)為jsonr = requests.post(url, data=json_data) # 使用requests的post方法,data接受你想發(fā)送的數(shù)據(jù)print(r.text) # 查看返回的內(nèi)容
輸出:
{ “args”: {}, “data”: “{”key2”: ”value2”, ”key1”: ”value1”}”, “files”: {}, “form”: {}, “headers”: { …… “Content-Type”: “application/json”, …… }, “json”: { “key1”: “value1”, “key2”: “value2” }, …… }
可以看到,請(qǐng)求頭的Content-Type設(shè)置為application/json,并將json_data這個(gè)json串提交到服務(wù)端中。
三、 以multipart形式發(fā)送post請(qǐng)求(上傳文件)
Requests也支持以multipart形式發(fā)送post請(qǐng)求,只需將一文件傳給requests.post()的files參數(shù)即可。
# -*- coding: utf-8 -*-# author:Garyimport requestsurl = ’http://httpbin.org/post’files = {’file’: open(’report.txt’, ’rb’)} # 目錄下得有report.txt文件才能上傳,rb是指以二進(jìn)制格式打開一個(gè)文件用于只讀。r = requests.post(url, files=files) # 通過files參數(shù)指定你想發(fā)送的文件的內(nèi)容print(r.text)
輸出:
{ “args”: {}, “data”: “”, “files”: { “file”: “Hello world!” }, “form”: {}, “headers”: {…… “Content-Type”: “multipart/form-data; boundary=467e443f4c3d403c8559e2ebd009bf4a”, …… }, “json”: null, …… }
文本文件report.txt的內(nèi)容只有一行:Hello world!,從請(qǐng)求的響應(yīng)結(jié)果可以看到數(shù)據(jù)已上傳到服務(wù)端中。
到此這篇關(guān)于python爬蟲使用requests發(fā)送post請(qǐng)求示例詳解的文章就介紹到這了,更多相關(guān)python爬蟲使用requests發(fā)送post請(qǐng)求內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. ASP中常用的22個(gè)FSO文件操作函數(shù)整理2. 無線標(biāo)記語言(WML)基礎(chǔ)之WMLScript 基礎(chǔ)第1/2頁(yè)3. ASP調(diào)用WebService轉(zhuǎn)化成JSON數(shù)據(jù),附j(luò)son.min.asp4. .Net core 的熱插拔機(jī)制的深入探索及卸載問題求救指南5. SharePoint Server 2019新特性介紹6. html清除浮動(dòng)的6種方法示例7. 讀大數(shù)據(jù)量的XML文件的讀取問題8. ASP.NET Core 5.0中的Host.CreateDefaultBuilder執(zhí)行過程解析9. React+umi+typeScript創(chuàng)建項(xiàng)目的過程10. Vue+elementUI下拉框自定義顏色選擇器方式
