hg3535_supplement.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. # -*- coding: utf-8 -*-
  2. # import jmespath
  3. import jsonpath
  4. import scrapy
  5. import json
  6. from scrapy.http import Request
  7. from scrapy_yzd.items import Hg3535Item, Today_all,Temaball
  8. class Hg3535Spider(scrapy.Spider):
  9. name = 'hg3535_supplement'
  10. allowed_domains = ['hg3535z.com']
  11. start_urls = ['http://hg3535z.com/odds2/d/getodds?sid=1&pt=4&ubt=am&pn=0&sb=2&dc=null&pid=0'] # 滚球菜单 足球滚球列url
  12. custom_settings = {
  13. "ITEM_PIPELINES": {
  14. "scrapy_yzd.pipelines.Temaballpipeline": 300,
  15. }
  16. }
  17. # start_urls = ['http://hg3535z.com/odds2/d/getodds?sid=3&pt=4&ubt=am&pn=0&sb=2&dc=null&pid=0'] # 滚球菜单 网球url
  18. # start_urls = ['http://hg3535z.com/odds2/d/getodds?sid=2&pt=4&ubt=am&pn=0&sb=2&dc=null&pid=0']
  19. # start_urls = ['http://hg3535z.com/odds2/d/getodds?sid=2&pt=4&ubt=am&pn=0&sb=2&dc=null&pid=0'] #篮球
  20. # start_urls = ['http://hg3535z.com/odds2/d/getodds?sid=1&pt=1&ubt=am&pn=0&sb=2&dc=null&pid=0'] # 今日足球
  21. def parse(self, response):
  22. datas = json.loads(response.text)
  23. # item = Today_all()
  24. ids = jsonpath.jsonpath(datas, '$..i-ot[0]..egs..es..i[16]') # ids新列表
  25. print(ids)
  26. if ids:
  27. ids = set(ids)
  28. for i in ids:
  29. urls = 'http://hg3535z.com/odds2/d/getamodds?eid={}&iip=true&ubt=am&isp=false'.format(i)
  30. print(urls)
  31. yield Request(url=urls, callback=self.parse_other)
  32. # ids = []
  33. #页面没数据异常处理
  34. # try:
  35. # results = datas['i-ot'][0]['egs']
  36. # except KeyError:
  37. # results = []
  38. # 第一
  39. # for result in results:
  40. # league_id = result['c']['k'] #联赛id
  41. # league_name = result['c']['n'] #联赛名
  42. # new_results = result['es'] # 取出列表嵌套字典
  43. # 二
  44. # for new_result in new_results:
  45. # ids = []
  46. # game_id = str(new_result['i'][16]) #比赛id
  47. # ids.append(game_id)
  48. # if ids:
  49. # ids1 = set(ids)#去重
  50. def parse_other(self, response):
  51. print(response)
  52. # 球队进球数 大小
  53. try:
  54. datas = json.loads(response.text)['eg']['es']
  55. league_id = json.loads(response.text)['eg']['c']['k']
  56. # 联赛名
  57. league_name = json.loads(response.text)['eg']['c']['n']
  58. except:
  59. datas = ""
  60. league_id = ""
  61. # 联赛名
  62. league_name = ""
  63. if datas:
  64. item = Temaball()
  65. full_dict = {}
  66. half_dict = {}
  67. full_dict_rule = {}
  68. half_dict_rule = {}
  69. data_list = []
  70. for data in datas:
  71. try:
  72. new_data = data['pci']['ctid']
  73. except:
  74. new_data = ""
  75. pass
  76. if new_data == 0:
  77. # 比赛id
  78. game_id = str(data['k'])
  79. data_list.append(game_id)
  80. # 球队1
  81. team_home = data['i'][0]
  82. data_list.append(team_home)
  83. # 球队2
  84. team_guest = data['i'][1]
  85. data_list.append(team_guest)
  86. # 数量(97>)
  87. number = data['i'][2]
  88. data_list.append(number)
  89. # 状态
  90. # zhuangtai = data['i'][3]
  91. # 日期
  92. data_game = data['i'][4]
  93. data_list.append(data_game)
  94. # 开赛时间
  95. time_game = data['i'][5]
  96. data_list.append(time_game)
  97. if new_data == 12:
  98. try:
  99. home_data = data['o']['ou']['v']
  100. # 球队进球 大条件
  101. home_size_big_rule = home_data[1]
  102. full_dict_rule['home_size_big'] = home_size_big_rule
  103. # 球队进球大赔率
  104. home_size_big = home_data[5]
  105. full_dict['home_size_big'] = home_size_big
  106. # 球队进球小条件
  107. home_size_small_rule = home_data[3]
  108. full_dict_rule['home_size_small'] = home_size_small_rule
  109. # 球队进球小赔率
  110. home_size_small = home_data[7]
  111. full_dict['home_size_small'] = home_size_small
  112. except:
  113. full_dict['home_size_small'] = ""
  114. full_dict_rule['home_size_small'] = ""
  115. full_dict['home_size_big'] = ""
  116. full_dict_rule['home_size_big'] = ""
  117. try:
  118. half_home_data = data['o']['ou1st']['v']
  119. half_home_size_big_rule = half_home_data[1]
  120. half_dict_rule['half_home_size_big'] = half_home_size_big_rule
  121. half_home_size_big = half_home_data[5]
  122. half_dict['half_home_size_big'] = half_home_size_big
  123. half_home_size_small_rule = half_home_data[3]
  124. half_dict_rule['half_home_size_small'] = half_home_size_small_rule
  125. half_home_size_small = half_home_data[7]
  126. half_dict['half_home_size_small'] = half_home_size_small
  127. except:
  128. half_dict_rule['half_home_size_big'] = ""
  129. half_dict['half_home_size_big'] = ""
  130. half_dict_rule['half_home_size_small'] = ""
  131. half_dict['half_home_size_small'] = ""
  132. if new_data == 13:
  133. try:
  134. guest_data = data['o']['ou']['v']
  135. guest_size_big_rule = guest_data[1]
  136. full_dict_rule['guest_size_big'] = guest_size_big_rule
  137. guest_size_big = guest_data[5]
  138. full_dict['guest_size_big'] = guest_size_big
  139. guest_size_small_rule = guest_data[3]
  140. full_dict_rule['guest_size_small'] = guest_size_small_rule
  141. guest_size_small = guest_data[7]
  142. full_dict['guest_size_small'] = guest_size_small
  143. except:
  144. full_dict_rule['guest_size_big'] = ""
  145. full_dict['guest_size_big'] = ""
  146. full_dict_rule['guest_size_small'] = ""
  147. full_dict['guest_size_small'] = ''
  148. try:
  149. half_guest_data = data['o']['ou1st']['v']
  150. half_guest_size_big_rule = half_guest_data[1]
  151. half_dict_rule['half_guest_size_big'] = half_guest_size_big_rule
  152. half_guest_size_big = half_guest_data[5]
  153. half_dict['half_guest_size_big'] = half_guest_size_big
  154. half_guest_size_small_rule = half_guest_data[3]
  155. half_dict_rule['half_guest_size_small'] = half_guest_size_small_rule
  156. half_guest_size_small = half_guest_data[7]
  157. half_dict['half_guest_size_small'] = half_guest_size_small
  158. except:
  159. half_dict_rule['half_guest_size_big'] = ""
  160. half_dict['half_guest_size_big'] = ""
  161. half_dict_rule['half_guest_size_small'] = ""
  162. half_dict['half_guest_size_small'] = ""
  163. #
  164. item['league_id'] = league_id
  165. item['league_name'] = league_name
  166. item['game_id'] = data_list[0]
  167. item['team_home'] = data_list[1]
  168. item['team_guest'] = data_list[2]
  169. item['number'] = data_list[3]
  170. item['data_game'] = data_list[4]
  171. item['time_game'] = data_list[5]
  172. item['full_data'] = full_dict
  173. item['half_data'] = half_dict
  174. item['full_data_rule'] = full_dict_rule
  175. item['half_data_rule'] = half_dict_rule
  176. yield item