roll_bangqiu.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. import datetime
  4. import jsonpath
  5. import redis
  6. __author__ = 'ethereal'
  7. __mtime__ = '2019/4/16'
  8. # -*- coding: utf-8 -*-
  9. import json
  10. import scrapy
  11. from scrapy.http import Request
  12. from ..items import Roll_Bangqiu
  13. class BqrangqiuSpider(scrapy.Spider):
  14. name = 'roll_bangqiu'
  15. to_day = datetime.datetime.now()
  16. allowed_domains = ['hg3535z.com']
  17. custom_settings = {
  18. "ITEM_PIPELINES": {
  19. # "hg3535.pipeline.Roll_Banqiupipeline": 300,
  20. "hg3535.pipeline.roll_bangqiu.Roll_Banqiupipeline": 350,
  21. },
  22. # 'LOG_LEVEL': 'DEBUG',
  23. # 'LOG_FILE': "../hg3535/log/roll_bangqiu_{}_{}_{}.log".format(to_day.year, to_day.month, to_day.day)
  24. }
  25. # start_urls = ['https://hg3535z.com/odds2/d/getodds?sid=4&pt=4&ubt=am&pn=0&sb=2&dc=null&pid=0'] # 滚球菜单 棒球滚球列url
  26. start_urls = [
  27. 'https://odata.jiushan6688.com/odds6i/d/getodds/zh-cn/sid/4/pt/4/ubt/am/pn/0/sb/2/dc/null/pid/0'] # 滚球菜单 棒球滚球列url
  28. rls = redis.Redis(host='192.168.2.200', port=6379, db=1, password=123456)
  29. def parse(self, response):
  30. datas = json.loads(response.text)
  31. ids = jsonpath.jsonpath(datas, '$..i-ot[0]..egs..es..i[16]') # ids新列表
  32. if ids:
  33. for i in ids:
  34. urls = 'https://odata.jiushan6688.com/odds6i/d/getamodds/zh-cn/eid/{}/iip/true/ubt/am/isp/false'.format(
  35. i)
  36. yield Request(url=urls, callback=self.parse_each, dont_filter=True)
  37. def parse_each(self, response):
  38. datas = json.loads(response.text)
  39. # 倒三角形 sa向下,sh向上
  40. triangle = datas['i'][5]
  41. try:
  42. new_datas = datas['eg']
  43. except:
  44. new_datas = ""
  45. try:
  46. pt = response.meta['pt']
  47. except:
  48. pt = 0
  49. if new_datas:
  50. # 联赛id
  51. league_id = new_datas["c"]["k"]
  52. # 联赛名
  53. league_name = new_datas["c"]["n"]
  54. new_data = new_datas["es"]
  55. for result in new_data:
  56. if result['pci']["ctid"] == 0:
  57. item = Roll_Bangqiu()
  58. # 比赛id
  59. game_id = str(result['k'])
  60. # 球队1
  61. team_home = result['i'][0]
  62. # 球队2
  63. team_guest = result['i'][1]
  64. # 数量(97>)
  65. number = result['i'][2]
  66. # 比赛状态
  67. zhuangtai = result['i'][3]
  68. # 日期
  69. data_game = result['i'][4]
  70. # 开赛时间
  71. time_game = result['i'][5]
  72. # 队1分数
  73. score_home1 = result['i'][10]
  74. # 队2分数
  75. score_guest1 = result['i'][11]
  76. # 第几盘
  77. # jijie = result['i'][12]
  78. # 球队得分
  79. qiudui = result['pci'].get('ctn', "")
  80. # 是否出局 不一定存在
  81. ot = result['sb'].get("ot", "")
  82. ps_list = result['sb']['ps']
  83. # 右菱形bool值
  84. fb = result['sb']['fb']
  85. # 左菱形bool值
  86. tb = result['sb']['tb']
  87. # 上菱形bool值
  88. sb = result['sb']['tb']
  89. # 局数或者节或者分
  90. cp = result['sb']['cp']
  91. # 第一个队伍
  92. a = result['sb']['a']
  93. # 第二个队伍
  94. h = result['sb']['h']
  95. d = {}
  96. d['ps'] = ps_list
  97. d['cp'] = cp
  98. d['ot'] = ot
  99. d['triangle'] = triangle
  100. d['fb'] = fb
  101. d['tb'] = tb
  102. d['sb'] = sb
  103. # --------------------------------------------------让球分割线----------------------------------------
  104. concedes_dict = {}
  105. concedes_dict_rule = {}
  106. try:
  107. concedes = result['o']["ah"]["v"]
  108. concedes_dict_rule['coh'] = concedes[1]
  109. concedes_dict['coh'] = concedes[5]
  110. concedes_dict_rule['cog'] = concedes[3]
  111. concedes_dict['cog'] = concedes[7]
  112. except:
  113. concedes_dict_rule['coh'] = ""
  114. concedes_dict['coh'] = ""
  115. concedes_dict_rule['cog'] = ""
  116. concedes_dict['cog'] = ""
  117. # 让球 前5局------------------------------------------------------------------------------------------
  118. try:
  119. concedes = result['o']["ahf5in"]["v"]
  120. concedes_dict_rule['coh_5'] = concedes[1]
  121. concedes_dict['coh_5'] = concedes[5]
  122. concedes_dict_rule['cog_5'] = concedes[3]
  123. concedes_dict['cog_5'] = concedes[7]
  124. except:
  125. concedes_dict_rule['coh_5'] = ""
  126. concedes_dict['coh_5'] = ""
  127. concedes_dict_rule['cog_5'] = ""
  128. concedes_dict['cog_5'] = ""
  129. # ---------------------------------------------------独赢--------------------------------------------
  130. capots_dict = {}
  131. try:
  132. capots = result['o']["ml"]["v"]
  133. capots_dict['ch'] = capots[1]
  134. capots_dict['cg'] = capots[3]
  135. except:
  136. capots_dict['ch'] = ""
  137. capots_dict['cg'] = ""
  138. # ---------------------------------------------------总得分:大/小-------------------------------------
  139. total_size_dict = {}
  140. total_size_dict_rule = {}
  141. try:
  142. total_sizes = result['o']["ou"]["v"]
  143. total_size_dict_rule['tnb'] = total_sizes[1]
  144. total_size_dict['tnb'] = total_sizes[5]
  145. total_size_dict_rule['tns'] = total_sizes[3]
  146. total_size_dict['tns'] = total_sizes[7]
  147. except:
  148. total_size_dict_rule['tnb'] = ""
  149. total_size_dict['tnb'] = ""
  150. total_size_dict_rule['tns'] = ""
  151. total_size_dict['tns'] = ""
  152. # 总得分: 大/小 前5局----------------------------------------------------------------------------------
  153. try:
  154. total_sizes = result['o']["ouf5in"]["v"]
  155. total_size_dict_rule['tnb_5'] = total_sizes[1]
  156. total_size_dict['tnb_5'] = total_sizes[5]
  157. total_size_dict_rule['tns_5'] = total_sizes[3]
  158. total_size_dict['tns_5'] = total_sizes[7]
  159. except:
  160. total_size_dict_rule['tnb_5'] = ""
  161. total_size_dict['tnb_5'] = ""
  162. total_size_dict_rule['tns_5'] = ""
  163. total_size_dict['tns_5'] = ""
  164. # ------------------------------------------------------总得分:单/双----------------------------------
  165. odd_evens_dict = {}
  166. odd_evens_dict_rule = {}
  167. try:
  168. odd_evens = result['o']["oe"]["v"]
  169. # 上半场 主队让球条件
  170. odd_evens_dict['tss'] = odd_evens[1]
  171. odd_evens_dict_rule['tss'] = "单"
  172. odd_evens_dict['tsd'] = odd_evens[3]
  173. odd_evens_dict_rule['tsd'] = "双"
  174. except:
  175. odd_evens_dict['tss'] = ""
  176. odd_evens_dict['tsd'] = ""
  177. odd_evens_dict_rule['tss'] = ""
  178. odd_evens_dict_rule['tsd'] = ""
  179. team_source_dict = {}
  180. try:
  181. team_sources = result['sb']['ps']
  182. for team_source in team_sources:
  183. if team_source['p'] == 'i1':
  184. team_source_dict['one_score'] = str(team_source['h']) + '-' + str(team_source['a'])
  185. if team_source['p'] == 'i2':
  186. team_source_dict['two_score'] = str(team_source['h']) + '-' + str(team_source['a'])
  187. if team_source['p'] == 'i3':
  188. team_source_dict['three_score'] = str(team_source['h']) + '-' + str(team_source['a'])
  189. if team_source['p'] == 'i4':
  190. team_source_dict['four_score'] = str(team_source['h']) + '-' + str(team_source['a'])
  191. if team_source['p'] == 'i5':
  192. team_source_dict['five_score'] = str(team_source['h']) + '-' + str(team_source['a'])
  193. if team_source['p'] == 'i6':
  194. team_source_dict['six_score'] = str(team_source['h']) + '-' + str(team_source['a'])
  195. if team_source['p'] == 'i7':
  196. team_source_dict['seven_score'] = str(team_source['h']) + '-' + str(team_source['a'])
  197. if team_source['p'] == 'i8':
  198. team_source_dict['eight_score'] = str(team_source['h']) + '-' + str(team_source['a'])
  199. if team_source['p'] == 'i9':
  200. team_source_dict['nine_score'] = str(team_source['h']) + '-' + str(team_source['a'])
  201. # 加时
  202. if team_source['p'] == 'ot':
  203. team_source_dict['overtime_score'] = str(team_source['h']) + '-' + str(team_source['a'])
  204. # 总局数比分
  205. if team_source['p'] == 'ft':
  206. team_source_dict['total_inning'] = int(team_source['h']) + int(team_source['a'])
  207. team_source_dict['total_source'] = str(team_source['h']) + '-' + str(team_source['a'])
  208. except:
  209. team_source_dict['one_score'] = ''
  210. team_source_dict['two_score'] = ''
  211. team_source_dict['three_score'] = ''
  212. team_source_dict['four_score'] = ''
  213. team_source_dict['five_score'] = ''
  214. team_source_dict['six_score'] = ''
  215. team_source_dict['seven_score'] = ''
  216. team_source_dict['eight_score'] = ''
  217. team_source_dict['nine_score'] = ''
  218. team_source_dict['overtime_score'] = ''
  219. team_source_dict['total_inning'] = 0
  220. team_source_dict['total_source'] = ''
  221. # 联赛id
  222. item['league_id'] = league_id
  223. # 联赛名
  224. item['league_name'] = league_name
  225. # 比赛id
  226. item['game_id'] = game_id
  227. # 球队1
  228. item['team_home'] = team_home
  229. # 球队2
  230. item['team_guest'] = team_guest
  231. # 数量(97>)
  232. item['number'] = number
  233. # 比赛状态
  234. item['zhuangtai'] = zhuangtai
  235. # 日期
  236. item['data_game'] = data_game
  237. # 开赛时间
  238. item['time_game'] = time_game
  239. # 队1分数
  240. item['score_home'] = score_home1
  241. # 队2分数
  242. item['score_guest'] = score_guest1
  243. # 第几节
  244. item['jijie'] = cp
  245. item['pt'] = pt
  246. # 球队得分
  247. item['qiudui'] = "{}:{}".format(a, h)
  248. # 让球
  249. item['concedes_dict'] = concedes_dict
  250. item['concedes_dict_rule'] = concedes_dict_rule
  251. # 独赢
  252. item['capots_dict'] = capots_dict
  253. # 总得分:大/小
  254. item['total_size_dict'] = total_size_dict
  255. item['total_size_dict_rule'] = total_size_dict_rule
  256. # 总得分:单/双
  257. item['odd_evens_dict'] = odd_evens_dict
  258. item['odd_evens_dict_rule'] = odd_evens_dict_rule
  259. item['match_score_dict'] = d
  260. item['team_source_dict'] = team_source_dict
  261. yield item