webapi.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. import requests, json
  2. from hytest.common import *
  3. from cfg import cfg
  4. from requests.packages import urllib3
  5. #存放公用方法
  6. # 存储 全局共享 数据
  7. GSTORE = {}
  8. class APIMgr():
  9. #打印https请求与消息
  10. def printRequest(self,req):
  11. if req.body==None:
  12. msgBody=''
  13. else:
  14. msgBody=req.body
  15. self.ui.outputWindow.append(
  16. '{}\n{}\n{}\n\n{}'.format(
  17. '\n\n-------发送请求--------',
  18. req.method+''+req.url,
  19. '\n'.join('{}:{}'.format(k,v) for k,v in req.headers.items()),
  20. msgBody,
  21. ))
  22. # 打印http相应消息的函数
  23. def printResponse(self, response):
  24. print('\n\n----- https response begin -----')
  25. print(response.status_code)
  26. # print(response.headers)
  27. for k, v in response.headers.items():
  28. print(f'{k}:{v}')
  29. print(response.content.decode('utf8'))
  30. print('----- https response end-----\n\n')
  31. headers = {
  32. "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"
  33. }
  34. #headers设置为全局变量
  35. GSTORE['headers'] = headers
  36. #登录接口
  37. def mgr_login(self, phone='18211989987', password='123456', useproxies=False):
  38. headers = GSTORE['headers']
  39. self.s = requests.Session()
  40. if useproxies:
  41. self.s.proxies.update({'http': '127.0.0.1:8888'})
  42. response = self.s.post(f"{cfg.target_host}/phone/login", headers=headers, data=
  43. {
  44. 'reqType': 'phoneLogin',
  45. 'isAutoLogin': 'false',
  46. 'phone': phone,
  47. 'password': password
  48. })
  49. self.printResponse(response)
  50. # Return the session object instead of the response
  51. return self.s
  52. #退出登录
  53. def mgr_logout(self, session):
  54. url = "https://www.jianyu360.cn/front/signOut"
  55. headers = {
  56. 'content-type': 'application/json',
  57. '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'
  58. }
  59. res = session.post(url=url, headers=headers)
  60. self.printResponse(res)
  61. return res
  62. # def mgr_login(self, phone='18211989987', password='123456',useproxies=False):
  63. # headers=GSTORE['headers']
  64. # self.s = requests.Session()
  65. # if useproxies:
  66. # self.s.proxies.update({'http':'127.0.0.1:8888'})
  67. # response = self.s.post(f"{cfg.target_host}/phone/login",headers=headers,data=
  68. # {
  69. # 'reqType': 'phoneLogin',
  70. # 'isAutoLogin': 'false',
  71. # 'phone':phone,
  72. # 'password':password
  73. # })
  74. # self.printResponse(response)
  75. # # 把response对象返回出去
  76. # return response
  77. #招标搜索
  78. def search(self, keywords="建筑", publishtime="fiveyear", selectType="content"):
  79. #使用全局变量
  80. headers = GSTORE['headers']
  81. #保存session
  82. session = self.s
  83. response = session.post(f"{cfg.target_host}/jylab/supsearch/index.html", headers=headers, data={
  84. 'keywords': keywords,
  85. 'publishtime': publishtime,
  86. 'selectType': selectType
  87. })
  88. self.printResponse(response)
  89. return response
  90. # 企业搜索
  91. def enterpriseSearch(self, match="北京剑鱼信息技术有限公司河南分公司", matchType="A", pageSize="10", pageNum="0"):
  92. # 使用全局变量
  93. headers = GSTORE['headers']
  94. # 保存session
  95. session = self.s
  96. response = session.post(f"{cfg.target_host}/publicapply/enterpriseSearch/doQuery", headers=headers, data={
  97. 'match': match,
  98. 'matchType': matchType,
  99. 'pageSize': pageSize,
  100. 'pageNum': pageNum
  101. })
  102. self.printResponse(response)
  103. return response
  104. # 供应搜索
  105. def supplySearch(self, keywords="PH计", searchType="title", province="", city="", time="", status="0",
  106. pageSize=50, pageIndex=1):
  107. headers = GSTORE['headers']
  108. headers['Content-Type'] = 'application/json' # 添加Content-Type头部
  109. url = f"{cfg.target_host}/jyinfo/supplySearch"
  110. data = {
  111. "keywords": keywords,
  112. "searchType": searchType,
  113. "province": province,
  114. "city": city,
  115. "time": time,
  116. "status": status,
  117. "pageSize": pageSize,
  118. "pageIndex": pageIndex
  119. }
  120. session=self.s
  121. response = session.post(url=url, headers=headers, data=json.dumps(data))
  122. self.printResponse(response)
  123. return response
  124. #采购单位搜索
  125. def buyersousuo(self, buyerName, province=[], city=[], buyerClass=[], isCheckFollow=True, isCheckReceive=True,
  126. isContact=0, pageSize=10,pageNum=1):
  127. url = f"{cfg.target_host}/jyapi/jybx/buyer/eType/buyerList"
  128. data = {
  129. "buyerName": buyerName,
  130. "province": province,
  131. "city": city,
  132. "buyerClass": buyerClass,
  133. "isCheckFollow": isCheckFollow,
  134. "isCheckReceive": isCheckReceive,
  135. "isContact": isContact,
  136. "pageSize": pageSize,
  137. "pageNum":pageNum
  138. }
  139. response = requests.post(url=url, data=json.dumps(data), headers=self.headers)
  140. self.printResponse(response)
  141. return response
  142. #不登录招标搜索
  143. def budenglu_search(keywords='数据', publishtime='thisyear', selectType='content,title'):
  144. headers = {
  145. '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'
  146. }
  147. params = {
  148. 'keywords': keywords,
  149. 'publishtime': publishtime,
  150. 'selectType': selectType
  151. }
  152. response = requests.get('https://www.jianyu360.cn/jylab/supsearch/index.html', headers=headers, params=params)
  153. response.raise_for_status() # 如果请求失败,会抛出异常
  154. return response
  155. #不登录采购单位搜索
  156. def budenglu_buysearch(searchvalue='北京大学', selectType='title'):
  157. headers = {
  158. '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'
  159. }
  160. params = {
  161. 'searchvalue': searchvalue,
  162. 'selectType': selectType
  163. }
  164. response = requests.get('https://www.jianyu360.cn/jylab/purSearch/index.html', headers=headers, params=params)
  165. response.raise_for_status() # 如果请求失败,会抛出异常
  166. return response
  167. apimgr = APIMgr()