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"}
'''
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)
PS:遇到問題沒人解答?需要Python學習資料?可以加點擊下方鏈接自行獲取
note.youdao.com/noteshare?id=2dce86d0c2588ae7c0a88bee34324d76
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"))