【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)
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"))

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM