wqbodan.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. # -*- coding: utf-8 -*-
  2. import copy
  3. import datetime
  4. import json
  5. import scrapy
  6. from scrapy.http import Request
  7. from ..items import Wqbodan
  8. class WgbodanSpider(scrapy.Spider):
  9. name = 'wqbodan'
  10. to_day = datetime.datetime.now()
  11. allowed_domains = ['hg3535z.com']
  12. custom_settings = {
  13. "ITEM_PIPELINES": {
  14. 'hg3535.pipelines.Wqbodanpipeline': 300,
  15. },
  16. 'LOG_LEVEL': 'DEBUG',
  17. 'LOG_FILE': "../hg3535/log/wqbodan_{}_{}_{}.log".format(to_day.year, to_day.month, to_day.day)
  18. }
  19. def start_requests(self):
  20. url = 'https://hg3535z.com/odds2/d/getmenu?pid=0'
  21. yield scrapy.Request(url=url, callback=self.parse_one, dont_filter=True)
  22. def parse_one(self, response):
  23. for y in range(1, 4):
  24. url = 'https://hg3535z.com/odds2/d/getodds?sid=3&pt=' + str(y) + '&ubt=stcs&pn=0&sb=2&dc=null&pid=0'
  25. yield Request(url=url, callback=self.parse, meta={'pt': y}, dont_filter=True)
  26. def parse(self, response):
  27. try:
  28. datas = json.loads(response.text)
  29. except:
  30. datas = ""
  31. try:
  32. results = datas['n-ot']['egs']
  33. except:
  34. results = ""
  35. try:
  36. pt = copy.copy(response.meta['pt'])
  37. except:
  38. pt = 0
  39. if results:
  40. for result in results:
  41. # 联赛id
  42. league_id = result['c']['k']
  43. # 联赛名
  44. league_name = result['c']['n']
  45. new_results = result['es']
  46. for new_result in new_results:
  47. item = Wqbodan()
  48. # 比赛id
  49. game_id = str(new_result['i'][16])
  50. # 球队1
  51. team_home = new_result['i'][0]
  52. # 球队2
  53. team_guest = new_result['i'][1]
  54. # 数量(97>)
  55. number = new_result['i'][2]
  56. # 状态
  57. zhuangtai = new_result['i'][3]
  58. # 日期
  59. data_game = new_result['i'][4]
  60. # 开赛时间
  61. time_game = new_result['i'][5]
  62. # 队1分数
  63. score_home = new_result['i'][10]
  64. # 队2分数
  65. score_guest = new_result['i'][11]
  66. # 下半场
  67. half_way = new_result['i'][12]
  68. # 角球或者其他
  69. corner_ball = new_result['pci'].get('ctn', "")
  70. bodan_data = {}
  71. # 波胆
  72. bodans = new_result['o'].get('sb', "")
  73. if bodans:
  74. one_list = ["bodanhome_two_zero", "bodanhome_two_one", "bodanhome_three_zero",
  75. "bodanhome_three_one", "bodanhome_three_two", "bodanhome_four_zero",
  76. "bodanhome_four_one", "bodanhome_four_two", "bodanhome_four_three"]
  77. two_list = ["bodanguest_two_zero", "bodanguest_two_one", "bodanguest_three_zero",
  78. "bodanguest_three_one", "bodanguest_three_two", "bodanguest_four_zero",
  79. "bodanguest_four_one", "bodanguest_four_two", "bodanguest_four_three"]
  80. new_bodans = [bodans[i] for i in range(len(bodans)) if i % 2 == 1]
  81. # 主队bodan_home
  82. bodan_home = [new_bodans[i] for i in range(len(new_bodans)) if i % 2 == 0]
  83. for index, t in enumerate(one_list):
  84. bodan_data[t] = bodan_home[index]
  85. # 客队bodan_guest
  86. bodan_guest = [new_bodans[i] for i in range(len(new_bodans)) if i % 2 == 1]
  87. for y, z in enumerate(two_list):
  88. bodan_data[z] = bodan_guest[y]
  89. item['league_id'] = league_id
  90. item['league_name'] = league_name
  91. item['game_id'] = game_id
  92. item['team_home'] = team_home
  93. item['team_guest'] = team_guest
  94. item['number'] = number
  95. item['data_game'] = data_game
  96. item['time_game'] = time_game
  97. item['corner_ball'] = corner_ball
  98. item['score_home'] = score_home
  99. item['score_guest'] = score_guest
  100. item['half_way'] = half_way
  101. item['pt'] = pt
  102. item['zhuangtai'] = zhuangtai
  103. item['bodan_data'] = bodan_data
  104. yield item