zhibo.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. # -*- coding: utf-8 -*-
  2. # import copy
  3. import datetime
  4. import json
  5. # import jsonpath
  6. # import time
  7. import redis
  8. import scrapy
  9. from scrapy.http import Request
  10. from ..settings import R_HOST, R_PASSWORD, R_POST, R_DB
  11. from ..items import FTzhibo
  12. class ZuqiuSpider(scrapy.Spider):
  13. name = 'zhibo'
  14. to_day = datetime.datetime.now()
  15. allowed_domains = ['hg3535z.com']
  16. custom_settings = {
  17. "ITEM_PIPELINES":{
  18. 'hg3535.pipeline.zhibo.Zuqiupipeline': 200,
  19. },
  20. # 'LOG_LEVEL': 'DEBUG',
  21. # 'LOG_FILE': "./log/FTzhibo_{}_{}_{}.log".format(to_day.year, to_day.month,to_day.day)
  22. }
  23. start_urls = ['https://odata.yonghuai5515.com/odds6i/d/getodds/zh-cn/sid/1/pt/4/ubt/am/pn/0/sb/2/dc/null/pid/0']
  24. rls = redis.Redis(host=R_HOST, port=R_POST, db=R_DB, password=R_PASSWORD)
  25. def parse(self, response):
  26. responses = json.loads(response.text)
  27. try:
  28. datas = responses["i-ot"]
  29. except:
  30. print("暂无滚球篮球数据")
  31. return
  32. if datas:
  33. for data in datas:
  34. egs = data.get('egs')
  35. if egs:
  36. for es in egs:
  37. es = es['es']
  38. for e in es:
  39. match_id = e['k']
  40. url = 'https://www.hg3535.cn/odds5/mid2rid/{}'.format(match_id)
  41. yield Request(url=url, callback=self.parse_one, dont_filter=True, meta={'match_id': match_id})
  42. else:
  43. print('足球滚球数据为空')
  44. else:
  45. print("暂无滚球足球数据")
  46. return
  47. def parse_one(self, response):
  48. # 球队进球数 大小
  49. r_data = json.loads(response.text)
  50. matchid = response.meta['match_id']
  51. if 'df' in r_data:
  52. live_id = r_data['id']
  53. live_url = 'https://erzhang1.com/188bet/zh/Etc:UTC/gismo/match_timelinedelta/{}'.format(live_id)
  54. yield Request(url=live_url, callback=self.parse_two, dont_filter=True, meta={'matchid': matchid})
  55. else:
  56. return
  57. def parse_two(self, response):
  58. mid = response.meta['matchid']
  59. response = json.loads(response.text)
  60. match_datas = response['doc']
  61. for match_data in match_datas:
  62. try:
  63. match = match_data['data']['match']
  64. except Exception as e:
  65. # print(e)
  66. return
  67. match_dict = match['teams']
  68. date = match['_dt']['uts']
  69. home = match_dict['home']['name']
  70. away = match_dict['away']['name']
  71. match_events = match_data['data']['events']
  72. if match_events:
  73. list_ball = []
  74. for match_event in match_events:
  75. event_type = match_event['type']
  76. if event_type == 'card':
  77. print('有卡牌, 事件')
  78. event_team = match_event['team']
  79. team_name = match_dict[event_team]['name']
  80. # 事件分钟
  81. # event_time = match_event['time']
  82. event_name = match_event['name']
  83. event_uts = match_event['uts']
  84. # updated_uts = match_event['updated_uts']
  85. # if match_event['card'] == 'red':
  86. # warn_type = 2
  87. # else:
  88. # warn_type = ''
  89. warn_type = 2
  90. list_ball.append((team_name, event_name, event_uts, warn_type))
  91. elif event_type == 'goal':
  92. print('有进球, 事件')
  93. event_team = match_event['team']
  94. team_name = match_dict[event_team]['name']
  95. # 事件分钟
  96. # event_time = match_event['time']
  97. event_name = match_event['name']
  98. event_uts = match_event['uts']
  99. # updated_uts = match_event['updated_uts']
  100. warn_type = 1
  101. list_ball.append((team_name, event_name, event_uts, warn_type))
  102. elif event_type == 'corner':
  103. print('有角球, 事件')
  104. event_team = match_event['team']
  105. team_name = match_dict[event_team]['name']
  106. # 事件分钟
  107. # event_time = match_event['time']
  108. event_name = match_event['name']
  109. event_uts = match_event['uts']
  110. warn_type = 3
  111. # updated_uts = match_event['updated_uts']
  112. list_ball.append((team_name, event_name, event_uts, warn_type))
  113. if list_ball:
  114. item = FTzhibo()
  115. item['detail'] = list_ball
  116. item['mid'] = mid
  117. item['date'] = date
  118. item['home'] = home
  119. item['away'] = away
  120. yield item
  121. else:
  122. print('当前时间, 无事件')
  123. return