python之多个接口公用登录态,session的使用

举例

class Check_tfurl():

    def __init__(self):
        self.urls=[]
        self.errors=[]
        self.ids=[]
        requests.packages.urllib3.disable_warnings()
        header = {
            "Referer": "xxx"
        }
        url = '登录接口'
        param = {"verifycode": "xx", "username": "xx", "password": "xxx", "cpinsadm_cryptCode": "xxx"}
        #调用登录接口,获取到登录态
        session = requests.Session()
        session.post(url, data=param, headers=header, verify=False)

        url = '登录后获取链接接口'
        for page in range(1,2):
            try:
                param = {"pageno": page, "t": "1585629902590"}
        #使用已经获取登录态的session去请求接下来的接口        
                res = session.get(url, params=param, headers=header, verify=False)
                res = res.json()
                if res["errno"]==0:
                    for i in range(len(res["record"])):
                        self.urls.append(res["record"][i]["tfurl_nologin"])
                        self.ids.append(res["record"][i]["id"])
                else:
                    self.errors.append(res["errno"])
                    self.errors.append(res["errmsg"])
            except Exception as msg:
                print(msg)

你可能感兴趣的:(python)