roll_bangqiu.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. import datetime
  4. import jsonpath
  5. __author__ = 'ethereal'
  6. __mtime__ = '2019/4/16'
  7. # -*- coding: utf-8 -*-
  8. import json
  9. import scrapy
  10. from scrapy.http import Request
  11. from ..items import Roll_Bangqiu
  12. class BqrangqiuSpider(scrapy.Spider):
  13. name = 'roll_bangqiu'
  14. to_day = datetime.datetime.now()
  15. allowed_domains = ['hg3535z.com']
  16. custom_settings = {
  17. "ITEM_PIPELINES": {
  18. # "hg3535.pipeline.Roll_Banqiupipeline": 300,
  19. "hg3535.pipeline.roll_bangqiu.Roll_Banqiupipeline": 350,
  20. },
  21. # 'LOG_LEVEL': 'DEBUG',
  22. # 'LOG_FILE': "../hg3535/log/roll_bangqiu_{}_{}_{}.log".format(to_day.year, to_day.month, to_day.day)
  23. }
  24. # start_urls = ['https://hg3535z.com/odds2/d/getodds?sid=4&pt=4&ubt=am&pn=0&sb=2&dc=null&pid=0'] # 滚球菜单 棒球滚球列url
  25. start_urls = [
  26. 'https://odata.jiushan6688.com/odds6i/d/getodds/zh-cn/sid/4/pt/4/ubt/am/pn/0/sb/2/dc/null/pid/0'] # 滚球菜单 棒球滚球列url
  27. # start_urls = ['http://hg3535z.com/odds2/d/getodds?sid=3&pt=4&ubt=am&pn=0&sb=2&dc=null&pid=0'] # 滚球菜单 网球测试滚球列url
  28. # start_urls = ['https://www.baidu.com']
  29. def parse(self, response):
  30. datas = json.loads(response.text)
  31. # urls = "https://www.baidu.com"
  32. # yield Request(url=urls,callback=self.parse_each)
  33. # datas ={"i": ["true", 4, 3110520, 2, 1, "sh", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "14 / 04", "", "", "false", "true", "\u68d2\u7403", 0], "eg": {"c": {"k": 29400, "n": "\u7f8e\u56fd\u804c\u4e1a\u68d2\u7403"}, "es": [{"dbg": "false", "egid": 0, "g": "", "i": ["\u4e9a\u7279\u5170\u5927\u52c7\u58eb\u961f", "\u7ebd\u7ea6\u5927\u90fd\u4f1a\u961f", "9", "True", "14 / 04", "", "9", "False", "0", "0", "2", "1", "", "", "", "", 0, "", "", 16, "True"], "ibs": "true", "ibsc": "false", "k": 3110520, "o": {"ah": {"s": 1, "v": ["h3164784133", "-1.5", "h3164784134", "+1.5", "o3164784133", "1.33", "o3164784134", "0.55"], "n": "\u6eda\u7403 \u8ba9\u7403"}, "ou": {"s": 2, "v": ["h3164784135", "6.5", "h3164784136", "6.5", "o3164784135", "0.70", "o3164784136", "1.04"], "n": "\u603b\u5f97\u5206:\u6eda\u7403 \u5927 / \u5c0f"}, "oe": {"s": 6, "v": ["o3164784137", "0.51", "o3164784138", "1.58"], "n": "\u603b\u5f97\u5206:\u6eda\u7403 \u5355 / \u53cc"}, "ml": {"s": 5, "v": ["o3164784139", "0.00", "o3164784140", "0.00"], "n": "\u6eda\u7403 \u72ec\u8d62\u76d8"}}, "p-o": [{"n": "\u51c0\u80dc\u7403\u6570", "st": "ha", "mgk": 9, "s": 7, "k": 301, "o": [["\u4e9a\u7279\u5170\u5927\u52c7\u58eb\u961f-\u8d62\u5f971\u5206", "o3164784186", "3.15", "0"], ["\u4e9a\u7279\u5170\u5927\u52c7\u58eb\u961f-\u8d62\u5f972\u5206", "o3164784187", "5.5", "3"], ["\u4e9a\u7279\u5170\u5927\u52c7\u58eb\u961f-\u8d62\u5f973\u5206", "o3164784188", "9.5", "6"], ["\u4e9a\u7279\u5170\u5927\u52c7\u58eb\u961f-\u8d62\u5f974\u5206\u6216\u66f4\u591a", "o3164784189", "7.5", "9"], ["\u7ebd\u7ea6\u5927\u90fd\u4f1a\u961f-\u8d62\u5f971\u5206", "o3164784190", "5.0", "2"], ["\u7ebd\u7ea6\u5927\u90fd\u4f1a\u961f-\u8d62\u5f972\u5206", "o3164784191", "7.5", "5"], ["\u7ebd\u7ea6\u5927\u90fd\u4f1a\u961f-\u8d62\u5f973\u5206", "o3164784192", "11.5", "8"], ["\u7ebd\u7ea6\u5927\u90fd\u4f1a\u961f-\u8d62\u5f974\u5206\u6216\u66f4\u591a", "o3164784193", "10.0", "11"]]}, {"n": "\u7b2c6\u5c40\u83b7\u80dc\u8005", "st": "ha", "mgk": 0, "s": 3000, "k": 4000, "o": [["\u548c\u5c40", "o3164784213", "1.50"], ["\u4e9a\u7279\u5170\u5927\u52c7\u58eb\u961f", "o3164784214", "5.5"], ["\u7ebd\u7ea6\u5927\u90fd\u4f1a\u961f", "o3164784215", "3.80"]]}, {"n": "\u52a0\u65f6", "st": "ha", "mgk": 312, "s": 8, "k": 13520, "o": [["\u662f", "o3164784228", "6.0"], ["\u4e0d\u662f", "o3164784229", "1.09"]]}], "pci": {"ctid": 0}, "sb": {"fb": "false", "sb": "false", "tb": "false", "ot": 0, "ps": [{"adv": -1, "a": 1.0, "e": 3110520, "h": 2.0, "p": "ft"}, {"adv": -1, "a": 0.0, "e": 3110520, "h": 0.0, "p": "ot"}, {"adv": -1, "a": 0.0, "e": 3110520, "h": 0.0, "p": "i1"}, {"adv": -1, "a": 1.0, "e": 3110520, "h": 1.0, "p": "i2"}, {"adv": -1, "a": 0.0, "e": 3110520, "h": 1.0, "p": "i3"}, {"adv": -1, "a": 0.0, "e": 3110520, "h": 0.0, "p": "i4"}, {"adv": -1, "a": 0.0, "e": 3110520, "h": 0.0, "p": "i5"}, {"adv": -1, "a": 0.0, "e": 3110520, "h": 0.0, "p": "i6"}, {"adv": -1, "a": 0.0, "e": 3110520, "h": 0.0, "p": "i7"}, {"adv": -1, "a": 0.0, "e": 3110520, "h": 0.0, "p": "i8"}, {"adv": -1, "a": 0.0, "e": 3110520, "h": 0.0, "p": "i9"}], "a": 1, "h": 2, "k": 3110520, "iwd": "false", "ls": -1, "s": 1, "ts": 0, "tt": 0, "ct": "00:00", "cp": "i5"}, "egn": ""}, {"dbg": "false", "egid": 0, "g": "", "i": ["ATL Braves - Team Runs - Over", "ATL Braves - Team Runs - Under", "9", "True", "14 / 04", "", "9", "False", "0", "0", "2", "0", "", "", "", "", 0, "", "", 16, "True"], "ibs": "true", "ibsc": "false", "k": 3110522, "o": {"ou": {"s": 3, "v": ["h3164784242", "3.5", "h3164784243", "3.5", "o3164784242", "1.12", "o3164784243", "0.63"], "n": "\u7403\u961f\u5f97\u5206:\u4e9a\u7279\u5170\u5927\u52c7\u58eb\u961f-\u6eda\u7403 \u5927 / \u5c0f"}}, "pci": {"ctid": 208, "ctn": "\u7403\u961f\u5f97\u5206"}, "egn": ""}, {"dbg": "false", "egid": 0, "g": "", "i": ["NY Mets - Team Runs - Over", "NY Mets - Team Runs - Under", "9", "True", "14 / 04", "", "9", "False", "0", "0", "1", "0", "", "", "", "", 0, "", "", 16, "True"], "ibs": "true", "ibsc": "false", "k": 3110524, "o": {"ou": {"s": 4, "v": ["h3164784261", "2.5", "h3164784263", "2.5", "o3164784261", "0.55", "o3164784263", "1.23"], "n": "\u7403\u961f\u5f97\u5206:\u7ebd\u7ea6\u5927\u90fd\u4f1a\u961f-\u6eda\u7403 \u5927 / \u5c0f"}}, "pci": {"ctid": 209, "ctn": "\u7403\u961f\u5f97\u5206"}, "egn": ""}]}, "ot": 2, "sc": {"3110520": {"a": 1, "h": 2}, "3110522": {"a": 0, "h": 2}, "3110524": {"a": 0, "h": 1}}, "v": 40576}
  34. # item = Today_all()
  35. ids = jsonpath.jsonpath(datas, '$..i-ot[0]..egs..es..i[16]') # ids新列表
  36. # print(ids)
  37. if ids:
  38. # ids = set(ids)
  39. for i in ids:
  40. # urls = 'https://hg3535z.com/odds2/d/getamodds?eid={}&iip=true&ubt=am&isp=false'.format(i)
  41. urls = 'https://odata.jiushan6688.com/odds6i/d/getamodds/zh-cn/eid/{}/iip/true/ubt/am/isp/false'.format(
  42. i)
  43. # print(urls)
  44. yield Request(url=urls, callback=self.parse_each, dont_filter=True)
  45. def parse_each(self, response):
  46. datas = json.loads(response.text)
  47. # 倒三角形 sa向下,sh向上
  48. triangle = datas['i'][5]
  49. try:
  50. # new_datas = json.loads(response.text)['eg']
  51. new_datas = datas['eg']
  52. except:
  53. new_datas = ""
  54. try:
  55. pt = response.meta['pt']
  56. except:
  57. pt = 0
  58. if new_datas:
  59. # 联赛id
  60. league_id = new_datas["c"]["k"]
  61. # 联赛名
  62. league_name = new_datas["c"]["n"]
  63. new_data = new_datas["es"]
  64. for result in new_data:
  65. if result['pci']["ctid"] == 0:
  66. item = Roll_Bangqiu()
  67. # 比赛id
  68. game_id = str(result['k'])
  69. # 球队1
  70. team_home = result['i'][0]
  71. # 球队2
  72. team_guest = result['i'][1]
  73. # 数量(97>)
  74. number = result['i'][2]
  75. # 比赛状态
  76. zhuangtai = result['i'][3]
  77. # 日期
  78. data_game = result['i'][4]
  79. # 开赛时间
  80. time_game = result['i'][5]
  81. # 队1分数
  82. score_home1 = result['i'][10]
  83. # 队2分数
  84. score_guest1 = result['i'][11]
  85. # 第几盘
  86. # jijie = result['i'][12]
  87. # 球队得分
  88. qiudui = result['pci'].get('ctn', "")
  89. # 是否出局 不一定存在
  90. ot = result['sb'].get("ot", "")
  91. ps_list = result['sb']['ps']
  92. # 右菱形bool值
  93. fb = result['sb']['fb']
  94. # 左菱形bool值
  95. tb = result['sb']['tb']
  96. # 上菱形bool值
  97. sb = result['sb']['tb']
  98. # 局数或者节或者分
  99. cp = result['sb']['cp']
  100. # 第一个队伍
  101. a = result['sb']['a']
  102. # 第二个队伍
  103. h = result['sb']['h']
  104. d = {}
  105. d['ps'] = ps_list
  106. d['cp'] = cp
  107. d['ot'] = ot
  108. d['triangle'] = triangle
  109. d['fb'] = fb
  110. d['tb'] = tb
  111. d['sb'] = sb
  112. # --------------------------------------------------让球分割线-----------------------------------------------------------
  113. concedes_dict = {}
  114. concedes_dict_rule = {}
  115. try:
  116. concedes = result['o']["ah"]["v"]
  117. concedes_dict_rule['coh'] = concedes[1]
  118. concedes_dict['coh'] = concedes[5]
  119. concedes_dict_rule['cog'] = concedes[3]
  120. concedes_dict['cog'] = concedes[7]
  121. except:
  122. concedes_dict_rule['coh'] = ""
  123. concedes_dict['coh'] = ""
  124. concedes_dict_rule['cog'] = ""
  125. concedes_dict['cog'] = ""
  126. # 让球 前5局-------------------------------------------------------------------------------------------------------------
  127. try:
  128. concedes = result['o']["ahf5in"]["v"]
  129. concedes_dict_rule['coh_5'] = concedes[1]
  130. concedes_dict['coh_5'] = concedes[5]
  131. concedes_dict_rule['cog_5'] = concedes[3]
  132. concedes_dict['cog_5'] = concedes[7]
  133. except:
  134. concedes_dict_rule['coh_5'] = ""
  135. concedes_dict['coh_5'] = ""
  136. concedes_dict_rule['cog_5'] = ""
  137. concedes_dict['cog_5'] = ""
  138. # ---------------------------------------------------独赢----------------------------------------------------------------
  139. capots_dict = {}
  140. try:
  141. capots = result['o']["ml"]["v"]
  142. capots_dict['ch'] = capots[1]
  143. capots_dict['cg'] = capots[3]
  144. except:
  145. capots_dict['ch'] = ""
  146. capots_dict['cg'] = ""
  147. # ---------------------------------------------------总得分:大/小--------------------------------------------------------
  148. total_size_dict = {}
  149. total_size_dict_rule = {}
  150. try:
  151. total_sizes = result['o']["ou"]["v"]
  152. total_size_dict_rule['tnb'] = total_sizes[1]
  153. total_size_dict['tnb'] = total_sizes[5]
  154. total_size_dict_rule['tns'] = total_sizes[3]
  155. total_size_dict['tns'] = total_sizes[7]
  156. except:
  157. total_size_dict_rule['tnb'] = ""
  158. total_size_dict['tnb'] = ""
  159. total_size_dict_rule['tns'] = ""
  160. total_size_dict['tns'] = ""
  161. # 总得分: 大/小 前5局-----------------------------------------------------------------------------------------------------
  162. try:
  163. total_sizes = result['o']["ouf5in"]["v"]
  164. total_size_dict_rule['tnb_5'] = total_sizes[1]
  165. total_size_dict['tnb_5'] = total_sizes[5]
  166. total_size_dict_rule['tns_5'] = total_sizes[3]
  167. total_size_dict['tns_5'] = total_sizes[7]
  168. except:
  169. total_size_dict_rule['tnb_5'] = ""
  170. total_size_dict['tnb_5'] = ""
  171. total_size_dict_rule['tns_5'] = ""
  172. total_size_dict['tns_5'] = ""
  173. # ------------------------------------------------------总得分:单/双-----------------------------------------------------
  174. odd_evens_dict = {}
  175. odd_evens_dict_rule = {}
  176. try:
  177. odd_evens = result['o']["oe"]["v"]
  178. # 上半场 主队让球条件
  179. odd_evens_dict['tss'] = odd_evens[1]
  180. odd_evens_dict_rule['tss'] = "单"
  181. odd_evens_dict['tsd'] = odd_evens[3]
  182. odd_evens_dict_rule['tsd'] = "双"
  183. except:
  184. odd_evens_dict['tss'] = ""
  185. odd_evens_dict['tsd'] = ""
  186. odd_evens_dict_rule['tss'] = ""
  187. odd_evens_dict_rule['tsd'] = ""
  188. team_source_dict = {}
  189. try:
  190. team_sources = result['sb']['ps']
  191. for team_source in team_sources:
  192. if team_source['p'] == 'i1':
  193. team_source_dict['one_score'] = str(team_source['h']) + '-' + str(team_source['a'])
  194. if team_source['p'] == 'i2':
  195. team_source_dict['two_score'] = str(team_source['h']) + '-' + str(team_source['a'])
  196. if team_source['p'] == 'i3':
  197. team_source_dict['three_score'] = str(team_source['h']) + '-' + str(team_source['a'])
  198. if team_source['p'] == 'i4':
  199. team_source_dict['four_score'] = str(team_source['h']) + '-' + str(team_source['a'])
  200. if team_source['p'] == 'i5':
  201. team_source_dict['five_score'] = str(team_source['h']) + '-' + str(team_source['a'])
  202. if team_source['p'] == 'i6':
  203. team_source_dict['six_score'] = str(team_source['h']) + '-' + str(team_source['a'])
  204. if team_source['p'] == 'i7':
  205. team_source_dict['seven_score'] = str(team_source['h']) + '-' + str(team_source['a'])
  206. if team_source['p'] == 'i8':
  207. team_source_dict['eight_score'] = str(team_source['h']) + '-' + str(team_source['a'])
  208. if team_source['p'] == 'i9':
  209. team_source_dict['nine_score'] = str(team_source['h']) + '-' + str(team_source['a'])
  210. # 加时
  211. if team_source['p'] == 'ot':
  212. team_source_dict['overtime_score'] = str(team_source['h']) + '-' + str(team_source['a'])
  213. # 总局数比分
  214. if team_source['p'] == 'ft':
  215. team_source_dict['total_inning'] = int(team_source['h']) + int(team_source['a'])
  216. team_source_dict['total_source'] = str(team_source['h']) + '-' + str(team_source['a'])
  217. except:
  218. team_source_dict['one_score'] = ''
  219. team_source_dict['two_score'] = ''
  220. team_source_dict['three_score'] = ''
  221. team_source_dict['four_score'] = ''
  222. team_source_dict['five_score'] = ''
  223. team_source_dict['six_score'] = ''
  224. team_source_dict['seven_score'] = ''
  225. team_source_dict['eight_score'] = ''
  226. team_source_dict['nine_score'] = ''
  227. team_source_dict['overtime_score'] = ''
  228. team_source_dict['total_inning'] = 0
  229. team_source_dict['total_source'] = ''
  230. # 联赛id
  231. item['league_id'] = league_id
  232. # 联赛名
  233. item['league_name'] = league_name
  234. # 比赛id
  235. item['game_id'] = game_id
  236. # 球队1
  237. item['team_home'] = team_home
  238. # 球队2
  239. item['team_guest'] = team_guest
  240. # 数量(97>)
  241. item['number'] = number
  242. # 比赛状态
  243. item['zhuangtai'] = zhuangtai
  244. # 日期
  245. item['data_game'] = data_game
  246. # 开赛时间
  247. item['time_game'] = time_game
  248. # 队1分数
  249. item['score_home'] = score_home1
  250. # 队2分数
  251. item['score_guest'] = score_guest1
  252. # 第几节
  253. item['jijie'] = cp
  254. item['pt'] = pt
  255. # 球队得分
  256. item['qiudui'] = "{}:{}".format(a, h)
  257. # 让球
  258. item['concedes_dict'] = concedes_dict
  259. item['concedes_dict_rule'] = concedes_dict_rule
  260. # 独赢
  261. item['capots_dict'] = capots_dict
  262. # 总得分:大/小
  263. item['total_size_dict'] = total_size_dict
  264. item['total_size_dict_rule'] = total_size_dict_rule
  265. # 总得分:单/双
  266. item['odd_evens_dict'] = odd_evens_dict
  267. item['odd_evens_dict_rule'] = odd_evens_dict_rule
  268. item['match_score_dict'] = d
  269. item['team_source_dict'] = team_source_dict
  270. yield item