1. 程式人生 > 實用技巧 >python中Requests的三種引數請求方式

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