python requests包的request()函數中的參數-params和data的區別介紹
如下所示:
import requests url=’http://www.baidu.com’#下面使用requests.request(method, url, **kwargs)re=requests.request(’GET’,url)
經驗證,可用。
我們試著傳入一個字典,首先用params參數。
結果為:
亮點在url和args。
我們還用get方法,把dic這個字典傳給data試試看。
亮點還是在args和url。驚喜地發現,dic這個字典沒傳進去。
這是因為:
params是用來發送查詢字符串,而data是用來發送正文的。post方法和get方法的特性是:這兩種參數post方法都可以用,get方法只能發查詢字符串,不能發送正文。
接下來試試看post方法:
上面這是用data參數傳字典的,亮點在form。
再試試用params參數傳這個字典:
亮點在url和args。
補充知識:python_request_三個參數
requests.request(method,url,**kwargs)
method:請求方法,對應get/put/post/delete/head/patch/options
url: 模擬獲取頁面的url連接
**kwrags:控制訪問的參數,共13個
kwargs(13個參數):
(一)params
params:字典或者字節序列,作為參數增加到url中
例子:
import requestskv={“wd”:“你好”}#拼接的內容用字典儲存r=requests.request(“GET”,“http://www.baidu.com/s”,params=kv)print(r.url)print(r.text)
運行后拼接的效果:http://www.baidu.com/s?wd=你好
(二)data
data:字典、字節、或文件對象,作為request
例子:
import requestskv={“key1”:“value1”,“key2”:“value2”}r=requests.request(“POST”,“http://httpbin.org/post”,data=kv)print(r.text)
運行結果:
{“args”: {},“data”: “”,“files”: {},“form”: {“key1”: “value1”,“key2”: “value2”},“headers”: {“Accept”: “/”,“Accept-Encoding”: “gzip, deflate”,“Connection”: “close”,“Content-Length”: “23”,“Content-Type”: “application/x-www-form-urlencoded”,“Host”: “httpbin.org”,“User-Agent”: “python-requests/2.18.1”},“json”: null,“origin”: “113.235.118.39”,“url”: “http://httpbin.org/post”}
(三)json
json:JSON格式的數據,作為request的內容
(四)header
header:字典,http定制頭
例子:
import requestshd={‘user-agent’:“Chrome/10”}#改變瀏覽器模擬r=requests.request(“post”,“http://www.baidu.com”,headers=hd
(五)cookies:
cookies:字典或CookieJar,request中的cookie
(六)auth
auth:元組,支持HTTP認證功能
(七)files:
files:字典類型,傳輸文件
(八)tiemout
timeout:設定時間
(九)proxies
proxies:字典類型,設定訪問代理服務器,可以增加登錄認證
以上這篇python requests包的request()函數中的參數-params和data的區別介紹就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持好吧啦網。
相關文章: