requests请求方法:requests.[method](url)
get:查看资源
post:增加资源
patch:修改资源
put:修改,替换资源
delete:删除
head:查看响应头
options:查看可用请求方法
github上有很多API,users
带参数的请求:服务端需要知道客户端的想要的内容,于是客户端需要上参数向服务端交互
url参数:参数在URL上。
requests.get(url,params={'key1','value1'})
表单参数:
json参数提交:
一个带参数请求实例:
import json import requests URL = 'https://api.github.com' #构建uri def build_uri(endpoint): return '/'.join([URL,endpoint]) def better_print(json_str): return json.dumps(json.loads(json_str),indent=4) def request_methoad(): #json参数请求 response1 = requests.post(build_uri('user/emails'),auth=('imoocdemo','imoocdemo123'),json= ["octocat20@github.com","support20@github.com"]) #无参请求 response2 = requests.get(build_uri('user/emails'),auth=('imoocdemo','imoocdemo123')) #url参数请求 response3 = requests.get(build_uri('users'),auth=('imoocdemo','imoocdemo123'),params={'since':'10'}) print(better_print(response1.text)) print(better_print(response2.text)) print(better_print(response3.text)) if __name__=='__main__': request_methoad()