bangqiu.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. # -*- coding: utf-8 -*-
  2. import copy
  3. import datetime
  4. import json
  5. import redis
  6. import scrapy
  7. from scrapy.http import Request
  8. from ..settings import R_HOST, R_PASSWORD, R_POST, R_DB
  9. from ..items import Bangqiu
  10. class BqrangqiuSpider(scrapy.Spider):
  11. name = 'bangqiu'
  12. to_day = datetime.datetime.now()
  13. allowed_domains = ['hg3535z.com']
  14. custom_settings = {
  15. "ITEM_PIPELINES": {
  16. "hg3535.pipeline.bangqiu.Bangqiupipeline": 300,
  17. },
  18. # 'LOG_LEVEL': 'DEBUG',
  19. # 'LOG_FILE': "../hg3535/log/bangqiu_{}_{}_{}.log".format(to_day.year, to_day.month, to_day.day)
  20. }
  21. rls = redis.Redis(host=R_HOST, port=R_POST, db=R_DB, password=R_PASSWORD)
  22. def start_requests(self):
  23. for y in range(1, 4):
  24. url = 'https://odata.yonghuai5515.com/odds6i/d/getodds/zh-cn/sid/4/pt/{}/ubt/am/pn/0/sb/2/dc/null/pid/0'.format(y)
  25. yield scrapy.Request(url=url, callback=self.parse, meta={'pt': y}, dont_filter=True)
  26. def parse(self, response):
  27. if response.text:
  28. try:
  29. datas = json.loads(response.text).get('n-ot', "").get('egs', "")
  30. except:
  31. datas = ""
  32. try:
  33. pt = copy.copy(response.meta['pt'])
  34. except:
  35. pt = 0
  36. if datas:
  37. for result in datas:
  38. new_results = result['es']
  39. for new_result in new_results:
  40. game_id = str(new_result['i'][16])
  41. if pt == 1:
  42. url = "https://odata.yonghuai5515.com/odds6i/d/getamodds/zh-cn/eid/{}/iip/false/ubt/am/isp/false".format(game_id)
  43. yield Request(url=url, callback=self.parse_each, meta={'pt': pt}, dont_filter=True)
  44. if pt == 2:
  45. url = "https://odata.yonghuai5515.com/odds6i/d/getamodds/zh-cn/eid/{}/iip/false/ubt/am/isp/false".format(game_id)
  46. yield Request(url=url, callback=self.parse_each, meta={'pt': pt}, dont_filter=True)
  47. if pt == 3:
  48. url = "https://odata.yonghuai5515.com/odds6i/d/getamodds/zh-cn/eid/{}/iip/false/ubt/am/isp/true".format(game_id)
  49. yield Request(url=url, callback=self.parse_each, meta={'pt': pt}, dont_filter=True)
  50. def parse_each(self, response):
  51. try:
  52. new_datas = json.loads(response.text)['eg']
  53. pt = response.meta['pt']
  54. bangqiu = json.loads(response.text)['i'][31]
  55. except:
  56. new_datas = ""
  57. pt = 0
  58. bangqiu = ""
  59. if new_datas:
  60. # 联赛id
  61. league_id = new_datas["c"]["k"]
  62. # 联赛名
  63. league_name = new_datas["c"]["n"]
  64. new_data = new_datas["es"]
  65. item = Bangqiu()
  66. for result in new_data:
  67. if result['pci']["ctid"] == 0:
  68. # 比赛id
  69. game_id = str(result['k'])
  70. # 球队1
  71. team_home = result['i'][0]
  72. # 球队2
  73. team_guest = result['i'][1]
  74. # 数量(97>)
  75. number = result['i'][2]
  76. # 比赛状态
  77. zhuangtai = result['i'][3]
  78. # 日期
  79. data_game = result['i'][4]
  80. # 开赛时间
  81. time_game = result['i'][5]
  82. # 队1分数
  83. score_home1 = result['i'][10]
  84. # 队2分数
  85. score_guest1 = result['i'][11]
  86. # 第几盘
  87. jijie = result['i'][12]
  88. # 球队得分
  89. qiudui = result['pci'].get('ctn', "")
  90. # --------------------------------------------------让球分割线-----------------------------------------------------------
  91. concedes_dict = {}
  92. concedes_dict_rule = {}
  93. try:
  94. concedes = result['o']["ah"]["v"]
  95. concedes_dict_rule['coh'] = concedes[1]
  96. concedes_dict_rule['cog'] = concedes[3]
  97. if pt is 3:
  98. concedes_dict['coh'] = float(concedes[5]) - 1
  99. concedes_dict['cog'] = float(concedes[7]) - 1
  100. else:
  101. concedes_dict['coh'] = float(concedes[5])
  102. concedes_dict['cog'] = float(concedes[7])
  103. except:
  104. concedes_dict_rule['coh'] = ""
  105. concedes_dict['coh'] = ""
  106. concedes_dict_rule['cog'] = ""
  107. concedes_dict['cog'] = ""
  108. # 让球 前5局-------------------------------------------------------------------------------------------------------------
  109. try:
  110. concedes = result['o']["ahf5in"]["v"]
  111. concedes_dict_rule['coh_5'] = concedes[1]
  112. concedes_dict_rule['cog_5'] = concedes[3]
  113. if pt is 3:
  114. concedes_dict['coh_5'] = float(concedes[5]) - 1
  115. concedes_dict['cog_5'] = float(concedes[7]) - 1
  116. else:
  117. concedes_dict['coh_5'] = concedes[5]
  118. concedes_dict['cog_5'] = concedes[7]
  119. except:
  120. concedes_dict_rule['coh_5'] = ""
  121. concedes_dict['coh_5'] = ""
  122. concedes_dict_rule['cog_5'] = ""
  123. concedes_dict['cog_5'] = ""
  124. # ---------------------------------------------------独赢----------------------------------------------------------------
  125. capots_dict = {}
  126. try:
  127. capots = result['o']["ml"]["v"]
  128. capots_dict['ch'] = capots[1]
  129. capots_dict['cg'] = capots[3]
  130. except:
  131. capots_dict['ch'] = ""
  132. capots_dict['cg'] = ""
  133. # ---------------------------------------------------总得分:大/小--------------------------------------------------------
  134. total_size_dict = {}
  135. total_size_dict_rule = {}
  136. try:
  137. total_sizes = result['o']["ou"]["v"]
  138. total_size_dict_rule['tnb'] = total_sizes[1]
  139. total_size_dict_rule['tns'] = total_sizes[3]
  140. if pt is 3:
  141. total_size_dict['tnb'] = float(total_sizes[5]) - 1
  142. total_size_dict['tns'] = float(total_sizes[7]) - 1
  143. else:
  144. total_size_dict['tnb'] = total_sizes[5]
  145. total_size_dict['tns'] = total_sizes[7]
  146. except:
  147. total_size_dict_rule['tnb'] = ""
  148. total_size_dict['tnb'] = ""
  149. total_size_dict_rule['tns'] = ""
  150. total_size_dict['tns'] = ""
  151. # 总得分: 大/小 前5局-----------------------------------------------------------------------------------------------------
  152. try:
  153. total_sizes = result['o']["ouf5in"]["v"]
  154. total_size_dict_rule['tnb_5'] = total_sizes[1]
  155. total_size_dict_rule['tns_5'] = total_sizes[3]
  156. if pt is 3:
  157. total_size_dict['tnb_5'] = float(total_sizes[5]) - 1
  158. total_size_dict['tns_5'] = float(total_sizes[7]) - 1
  159. else:
  160. total_size_dict['tnb_5'] = total_sizes[5]
  161. total_size_dict['tns_5'] = total_sizes[7]
  162. except:
  163. total_size_dict_rule['tnb_5'] = ""
  164. total_size_dict['tnb_5'] = ""
  165. total_size_dict_rule['tns_5'] = ""
  166. total_size_dict['tns_5'] = ""
  167. # ------------------------------------------------------总得分:单/双-----------------------------------------------------
  168. odd_evens_dict = {}
  169. odd_evens_dict_rule = {}
  170. try:
  171. odd_evens = result['o']["oe"]["v"]
  172. # 上半场 主队让球条件
  173. odd_evens_dict_rule['tss'] = "单"
  174. odd_evens_dict_rule['tsd'] = "双"
  175. if pt is 3:
  176. odd_evens_dict['tss'] = float(odd_evens[1]) - 1
  177. odd_evens_dict['tsd'] = float(odd_evens[3]) - 1
  178. else:
  179. odd_evens_dict['tss'] = odd_evens[1]
  180. odd_evens_dict['tsd'] = odd_evens[3]
  181. except:
  182. odd_evens_dict['tss'] = ""
  183. odd_evens_dict['tsd'] = ""
  184. odd_evens_dict_rule['tss'] = ""
  185. odd_evens_dict_rule['tsd'] = ""
  186. # 联赛id
  187. item['league_id'] = league_id
  188. # 联赛名
  189. item['league_name'] = league_name
  190. # 比赛id
  191. item['game_id'] = game_id
  192. # 球队1
  193. item['team_home'] = team_home
  194. # 球队2
  195. item['team_guest'] = team_guest
  196. # 数量(97>)
  197. item['number'] = number
  198. # 比赛状态
  199. item['zhuangtai'] = zhuangtai
  200. # 日期
  201. item['data_game'] = data_game
  202. # 开赛时间
  203. item['time_game'] = time_game
  204. # 队1分数
  205. item['score_home'] = score_home1
  206. # 队2分数
  207. item['score_guest'] = score_guest1
  208. # 第几节
  209. item['jijie'] = jijie
  210. item['pt'] = pt
  211. # 球队得分
  212. item['qiudui'] = qiudui
  213. # 让球
  214. item['concedes_dict'] = concedes_dict
  215. item['concedes_dict_rule'] = concedes_dict_rule
  216. # 独赢
  217. item['capots_dict'] = capots_dict
  218. # 总得分:大/小
  219. item['total_size_dict'] = total_size_dict
  220. item['total_size_dict_rule'] = total_size_dict_rule
  221. # 总得分:单/双
  222. item['odd_evens_dict'] = odd_evens_dict
  223. item['odd_evens_dict_rule'] = odd_evens_dict_rule
  224. item['bangqiu'] = bangqiu
  225. yield item