import requests, json from hytest.common import * from cfg import cfg from requests.packages import urllib3 #存放公用方法 # 存储 全局共享 数据 GSTORE = {} class APIMgr(): #打印https请求与消息 def printRequest(self,req): if req.body==None: msgBody='' else: msgBody=req.body self.ui.outputWindow.append( '{}\n{}\n{}\n\n{}'.format( '\n\n-------发送请求--------', req.method+''+req.url, '\n'.join('{}:{}'.format(k,v) for k,v in req.headers.items()), msgBody, )) # 打印http相应消息的函数 def printResponse(self, response): print('\n\n----- https response begin -----') print(response.status_code) # print(response.headers) for k, v in response.headers.items(): print(f'{k}:{v}') print(response.content.decode('utf8')) print('----- https response end-----\n\n') headers = { "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36 Edg/114.0.1823.67" } #headers设置为全局变量 GSTORE['headers'] = headers #登录接口 def mgr_login(self, phone='18211989987', password='123456', useproxies=False): headers = GSTORE['headers'] self.s = requests.Session() if useproxies: self.s.proxies.update({'http': '127.0.0.1:8888'}) response = self.s.post(f"{cfg.target_host}/phone/login", headers=headers, data= { 'reqType': 'phoneLogin', 'isAutoLogin': 'false', 'phone': phone, 'password': password }) self.printResponse(response) # Return the session object instead of the response return self.s #退出登录 def mgr_logout(self, session): url = "https://www.jianyu360.cn/front/signOut" headers = { 'content-type': 'application/json', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3' } res = session.post(url=url, headers=headers) self.printResponse(res) return res # def mgr_login(self, phone='18211989987', password='123456',useproxies=False): # headers=GSTORE['headers'] # self.s = requests.Session() # if useproxies: # self.s.proxies.update({'http':'127.0.0.1:8888'}) # response = self.s.post(f"{cfg.target_host}/phone/login",headers=headers,data= # { # 'reqType': 'phoneLogin', # 'isAutoLogin': 'false', # 'phone':phone, # 'password':password # }) # self.printResponse(response) # # 把response对象返回出去 # return response #招标搜索 def search(self, keywords="建筑", publishtime="fiveyear", selectType="content"): #使用全局变量 headers = GSTORE['headers'] #保存session session = self.s response = session.post(f"{cfg.target_host}/jylab/supsearch/index.html", headers=headers, data={ 'keywords': keywords, 'publishtime': publishtime, 'selectType': selectType }) self.printResponse(response) return response # 企业搜索 def enterpriseSearch(self, match="北京剑鱼信息技术有限公司河南分公司", matchType="A", pageSize="10", pageNum="0"): # 使用全局变量 headers = GSTORE['headers'] # 保存session session = self.s response = session.post(f"{cfg.target_host}/publicapply/enterpriseSearch/doQuery", headers=headers, data={ 'match': match, 'matchType': matchType, 'pageSize': pageSize, 'pageNum': pageNum }) self.printResponse(response) return response # 供应搜索 def supplySearch(self, keywords="PH计", searchType="title", province="", city="", time="", status="0", pageSize=50, pageIndex=1): headers = GSTORE['headers'] headers['Content-Type'] = 'application/json' # 添加Content-Type头部 url = f"{cfg.target_host}/jyinfo/supplySearch" data = { "keywords": keywords, "searchType": searchType, "province": province, "city": city, "time": time, "status": status, "pageSize": pageSize, "pageIndex": pageIndex } session=self.s response = session.post(url=url, headers=headers, data=json.dumps(data)) self.printResponse(response) return response #采购单位搜索 def buyersousuo(self, buyerName, province=[], city=[], buyerClass=[], isCheckFollow=True, isCheckReceive=True, isContact=0, pageSize=10,pageNum=1): url = f"{cfg.target_host}/jyapi/jybx/buyer/eType/buyerList" data = { "buyerName": buyerName, "province": province, "city": city, "buyerClass": buyerClass, "isCheckFollow": isCheckFollow, "isCheckReceive": isCheckReceive, "isContact": isContact, "pageSize": pageSize, "pageNum":pageNum } response = requests.post(url=url, data=json.dumps(data), headers=self.headers) self.printResponse(response) return response #不登录招标搜索 def budenglu_search(keywords='数据', publishtime='thisyear', selectType='content,title'): headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3' } params = { 'keywords': keywords, 'publishtime': publishtime, 'selectType': selectType } response = requests.get('https://www.jianyu360.cn/jylab/supsearch/index.html', headers=headers, params=params) response.raise_for_status() # 如果请求失败,会抛出异常 return response #不登录采购单位搜索 def budenglu_buysearch(searchvalue='北京大学', selectType='title'): headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3' } params = { 'searchvalue': searchvalue, 'selectType': selectType } response = requests.get('https://www.jianyu360.cn/jylab/purSearch/index.html', headers=headers, params=params) response.raise_for_status() # 如果请求失败,会抛出异常 return response apimgr = APIMgr()