使用requests.session()获取不到cookie怎么办?

有时候,用requests.session()登录,获取不到cookie,要使用原始方式获取cookie:

    def login(self):
        '''
        登录正式环境
        :return:
        '''
        self._session = requests.session()
        url = "http://cdht-sentinel.bbdservice.com/api/v1.0/auth/sso/login?t=55515"
        data = {
            "ip": "182.150.28.190",
            "location": "四川省成都市",
            "password": "71b44033d3814ca82c7d079e046d6765",
            "systemState": 2,
            "username": "admin2"
        }
        # 登录
        response = requests.post(url=url, data=json.dumps(data), headers=self._headers)
        ret = json.loads(response.text)
        cookie_str = response.headers.get("Set-Cookie")
        cookie_tuple = cookie_str.split(";")
        for item in cookie_tuple:
            if item.count(",") >= 1:
                for item1 in item.split(","):
                    key1, value1 = item1.split("=")
                    self._cookies[key1] = value1
            else:
                key, value = item.split("=")
                self._cookies[key] = value
        print("登录成功?:", self._cookies)
原文地址:https://www.cnblogs.com/staff/p/14275530.html