python教程:Requests的三種參數請求方式

URL參數請求:

import requests
 
'''
URL Parameters 請求方式: URL參數
例如: 以get 方式請求http://httpbin.org/get?first_name=hello&last_name=word
 
'''
# params={"first_name":"hello","last_name":"word"}
# responds=requests.get("http://httpbin.org/get",params=params)
# print(responds.text)
# print(responds.url)

表單參數請求:

'''
表單參數提交,注意只更新的是表單數據
Content-Type: application/x-www-form-urlencoded
例如: 以post 方式請求http://httpbin.org/post, 
增加的資源爲params={"first_name":"hello","last_name":"word"}
遇到問題沒人解答?小編創建了一個Python學習交流QQ羣:531509025
尋找有志同道合的小夥伴,互幫互助,羣裏還有不錯的視頻學習教程和PDF電子書!
'''
params={"first_name":"hello","last_name":"word"}
headers={"Content-Tpye":"application/x-www-form-urlencoded"}
responds=requests.post("http://httpbin.org/post",data=params,headers=headers)
print(responds.text)
print(responds.url)
print(responds.request)

Json 參數數據請求

'''
Json數據提交,更新的是數據data
Content-Type: application/json
例如: 以post 方式請求http://httpbin.org/post, 增加的資源爲params={"first_name":"hello","last_name":"word"}
 
'''
params={"first_name":"hello","last_name":"word"}
headers={"Content-Tpye":"application/json"}
responds=requests.post("http://httpbin.org/post",json=params,headers=headers)
print(responds.text)
print(responds.url)
print(responds.request)

額外話題–構建URL

def build_url(URL,endpoint):
    return "/".join([URL,endpoint])
print(build_url("http://httpbin.org","post"))
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章