wqbodan.py 4.5 KB

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