使用unittest方法写登录接口,调用cookie
#!/usr/bin/env python3 # -*- coding: utf-8 -*- import unittest import requests import re import warnings class HouseTest(unittest.TestCase): # 初始化方法 def setUp(self) -> None: warnings.simplefilter('ignore', ResourceWarning) login_url = 'http://uat.cbs.bacic5i5j.com/cas/login?service=http://uat.cbs.bacic5i5j.com/base/cas' res = requests.get(login_url) if res.status_code not in [200, 202]: print('login failed') raise res_data = res.text self.lt = re.findall('name="lt" value="(.+?)"',res_data)[0] self.execution = re.findall('name="execution" value="(.+?)"', res_data)[0] self.event_id = re.findall('name="_eventId" value="(.+?)"', res_data)[0] self.cookie = '' for key, val in res.cookies.items(): self.cookie += key + '=' + val + ';' self.headers = {'Content-Type': 'application/x-www-form-urlencoded', 'cookie': self.cookie} data = {'lt': self.lt, 'execution': self.execution, '_eventId': self.event_id, 'username': '509548', 'password':'1q2w3e4r'} self.session = requests.session() self.res2 = self.session.post(login_url, params=data, headers=self.headers) # print(self.res2.text) def test_login(self): pass # 网签待核对列表 def test_buildlist(self): url2 = 'http://uat.cbs.bacic5i5j.com/bdc/bdcNetSignDb/task_getData.htm' data = { 'offset': 15, #第一页是0 'pageSize': 15, 'sortOrder': 'asc', } headers = { 'Content-Type': 'application/x-www-form-urlencoded', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.82 Safari/537.36', 'cookie': self.cookie } # 不用添加header了,会记住第一次的header信息 res3 = self.session.post(url2, json=data) print(res3.status_code) print(res3.text) if '待核对' in res3.text: print('已经进入了我要找的页面') else: print('操作有误') # 销毁函数 def tearDown(self) -> None: pass if __name__ == '__main__': unittest.main()