zhibo.py 5.1 KB

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