params参数请求实例

 1 #!/usr/bin/python
 2 # -*- coding: UTF-8 -*-
 3 import json
 4 import requests
 5 
 6 '''Params参数请求举例'''
 7 
 8 URL = "https://api.github.com"
 9 
10 #拼接接口请求地址
11 def build_url(endpoint):
12     return '/'.join([URL,endpoint])
13 
14 #使用json里面提供方法打印出来,格式更好看
15 def better_output(json_str):
16     return json.dumps(json.loads(json_str),indent=4)
17 
18 # 调用get方法,注意用户名这个地方写法,没有图片中冒号
19 def request_method():
20     response = requests.get(build_url("users/Anthonyliu86"))
21     print(better_output(response.text)) # 调用json更好格式输出
22 
23 def params_method():
24     response = requests.get(build_url("users"),params={'since':11})
25     print(better_output(response.text))
26     print(response.headers)
27 
28 if __name__=='__main__':
29     params_method()

 

posted @ 2018-08-28 16:24  jaysonzxk  阅读(1676)  评论(0编辑  收藏  举报