MOOC(7)- case依赖、读取json配置文件进行多个接口请求-封装接口请求(1)

 
 
# 封装接口请求
import requests
import json


def send_get(url, para):
res = requests.get(url, para)
return res.json()


def send_post(url, data):
res = requests.post(url, data)
r = res.json()
# json.dumps使得输出的字典或json数据更美观,这里的参数r可以是字典,可以是json
return json.dumps(r, indent=2, sort_keys=True, ensure_ascii=False)


def run_main(url, method, data=None):
res = None
if method.upper() == "GET":
res = send_get(url, data)
elif method.upper() == "POST":
res = send_post(url, data)
else:
print("请输入正确的参数")
return res


url_1 = "http://apis.juhe.cn/rubbish/search"
data = {"key": "6d9cc6b16d6cf63caded401b99c7311e",
"q": "奶茶",
"type": 1}
r = run_main(url_1, "post", data)
print(r)

猜你喜欢

转载自www.cnblogs.com/come202011/p/12313080.html
今日推荐