wqbodan.py 4.5 KB

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