使用类封装接口

import requests
import json

class RunMain:
    def __init__(self,url,method,data=None):
        self.res = self.run_main(url,method,data)

    def send_post(self,url,data):
        res = requests.post(url,data)
        return json.dumps(res,indent=2,sort_keys=True)

    def send_get(self,url,data):
        res = requests.get(url,data).json()
        return json.dumps(res,indent=2,sort_keys=True)

    def run_main(self,url,method,data):
        if method == 'GET':
            res = self.send_get(url,data)
        else:
            res = self.send_post(url,data)
        return res

if __name__=="__main__":
    url = "https://www.apiopen.top/journalismApi"
    run = RunMain(url,"GET")

    print(run.res)

你可能感兴趣的:(使用类封装接口)