lq_sports.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. # -*- coding: utf-8 -*-
  2. import scrapy
  3. from ..items import LanqiuItem
  4. import copy
  5. import lxml.etree
  6. import re,os,json
  7. from ..utils.helper import Helper
  8. import time
  9. from ..items import LanqiuItem
  10. import xmltodict
  11. class LqSportsSpider(scrapy.Spider):
  12. name = 'lq_sports'
  13. allowed_domains = ['m.hgg070.com/']
  14. start_urls = ['http://m.hgg070.com//']
  15. remath = re.compile("篮球")
  16. # custom_settings={
  17. # "ITEM_PIPELINES": {
  18. # "hgg070_spider.pipelines.lq_sports.LqSportsPipeline": 200,
  19. # },
  20. # }
  21. def start_requests(self):
  22. #今日,早盘
  23. h_types=[('FT'),('FU')]
  24. headers = {
  25. 'Accept': '*/*',
  26. 'Accept-Encoding': 'gzip, deflate',
  27. 'Accept-Language': 'zh-CN,zh;q=0.9',
  28. 'Connection': 'keep-alive',
  29. 'Content-Length': '130',
  30. 'Content-type': 'application/x-www-form-urlencoded',
  31. 'Cookie': '_ga=GA1.2.471918301.1572059707; _gid=GA1.2.2109447865.1572059707; _gat=1',
  32. 'Host': 'm.hgg070.com',
  33. 'Origin': 'http://m.hgg070.com',
  34. 'Referer': 'http://m.hgg070.com/',
  35. 'User-Agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.70 Mobile Safari/537.36'
  36. }
  37. url = "http://m.hgg070.com/app/member/get_league_list.php"
  38. for item in h_types:
  39. showtype = item
  40. data={
  41. 'uid': '3970335d20df9b8ceca8673ae9b6ea910c912492f595c0ef163623ae0ea883b6',
  42. 'langx': 'zh-cn',
  43. 'ltype': '3',
  44. 'gtype': 'BK',
  45. 'showtype': showtype,
  46. 'sorttype': '',
  47. 'date': '',
  48. 'isP': ''
  49. }
  50. yield scrapy.FormRequest(url=url,formdata=data,callback=self.parse,headers=headers,
  51. meta={"data":data}, dont_filter=True)
  52. def parse(self, response):
  53. #获取id并判断抓取的球型
  54. data=response.meta["data"]
  55. fromdata=copy.deepcopy(data)
  56. league=response.xpath('//league')
  57. url="http://m.hgg070.com/app/member/get_game_list.php"
  58. for le in league:
  59. name=le.xpath('./league_name/text()').extract_first()
  60. if len(self.remath.findall(name))>0:
  61. lid = le.xpath('./league_id/text()').extract_first()
  62. # 抓取今日
  63. if data["showtype"]=="FT":
  64. data['lid'],data['sorttype'],data['date']=lid,'league',''
  65. # 抓取早盘
  66. elif data["showtype"]=="FU":
  67. data['lid'], data['sorttype'], data['date'] = lid, 'league', 'all'
  68. yield scrapy.FormRequest(url=url,formdata=data,callback=self.detailball,meta={"data":fromdata},dont_filter=True)
  69. def detailball(self,response):
  70. data=response.meta["data"]
  71. url="http://m.hgg070.com/app/member/get_game_more.php"
  72. #获取联赛id gid
  73. game=response.xpath("//game")
  74. for g in game:
  75. gid=g.xpath("./gid/text()").extract_first()
  76. more_count = g.xpath("./more_count/text()").extract_first()
  77. data["gid"]=gid
  78. yield scrapy.FormRequest(url=url,formdata=data,callback=self.getItem,meta={"more_count":more_count,"isP":data["isP"]},dont_filter=True)
  79. def getItem(self,response):
  80. more_count = response.meta["more_count"]
  81. isP = response.meta["isP"]
  82. data= xmltodict.parse(response.text)['serverresponse']['game']
  83. game_lists=[i for i in data if i['gopen']=='Y']
  84. if game_lists:
  85. for gl in game_lists:
  86. cpath=os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
  87. with open(cpath+"/conf/hgg070.json",encoding='utf8') as hg:
  88. hgg=json.load(hg)['bk']
  89. datetime = gl['datetime'][:-8] + " " + gl['datetime'][-8:]
  90. team_h = gl['team_h']
  91. team_c = gl['team_c']
  92. league_id = gl['gidm']
  93. match_id = gl.get('gid', '')
  94. match_uid = Helper.genearte_uuid(team_h + team_c + datetime)
  95. data = []
  96. for hg in hgg:
  97. items=hg['items']
  98. if gl[hg['prodds']]=='Y':
  99. for x in items:
  100. odds_code = gl[x['rodds']]
  101. p_code = gl[hg['prodds']]
  102. odds=gl["ior_OUH"]
  103. #有两个条件,加两条数据
  104. if x['ratio_name']: #大的
  105. condition_u=gl[x['ratio_name']]
  106. odds_only = hg["plodds"] + x["lodds"] + '0' + condition_u + str(odds) + "hg3535" + str(match_id)
  107. sole = hg["plodds"] + x["lodds"] + '0' + str(match_id) + "hg3535"
  108. tobj = {"match_id": match_id, "lg_id": league_id, "odds_code": odds_code, "status": 0,
  109. "sort": 0, "p_code": p_code,
  110. "odds": odds, "condition": condition_u, "odds_only": odds_only, "sole": sole,
  111. "source": "hgg070", "type": 0, "team": ""}
  112. data.append(tobj)
  113. if x['latio']: #小的
  114. condition_s = gl[x['latio']]
  115. odds_only =hg["plodds"] + x["lodds"] + '0' +condition_s + str(odds) + "hg3535" + str(match_id)
  116. sole = hg["plodds"] + x["lodds"] + '0' + str(match_id) + "hg3535"
  117. tobj = {"match_id": match_id, "lg_id": league_id, "odds_code": odds_code, "status": 0,
  118. "sort": 0, "p_code": p_code,
  119. "odds": odds,"condition": condition_s, "odds_only": odds_only, "sole": sole,
  120. "source": "hgg070", "type": 0, "team": ""}
  121. data.append(tobj)
  122. if not x['latio'] and not x['ratio_name']:
  123. condition_s = ''
  124. odds_only = hg["plodds"] + x["lodds"] + '0' +condition_s + str(odds) + "hg3535" + str(match_id)
  125. sole = hg["plodds"] + x["lodds"] + '0' + str(match_id) + "hg3535"
  126. tobj = {"match_id": match_id, "lg_id": league_id, "odds_code": odds_code, "status": 0,
  127. "sort": 0, "p_code": p_code,
  128. "odds": odds,"condition": condition_s, "odds_only": odds_only, "sole": sole,
  129. "source": "hgg070", "type": 0, "team": ""}
  130. data.append(tobj)
  131. item = LanqiuItem()
  132. item['match_id'] = match_id
  133. item['source'] = "hg0088"
  134. item['updata'] = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  135. item['content'] = data
  136. item['league_id'] = league_id
  137. item['more_count'] = more_count
  138. item['league'] = gl["league"]
  139. item['match_identity'] = match_uid
  140. item['datetime'] = datetime
  141. item['team_h'] = team_h
  142. item['team_c'] = team_c
  143. item['isP'] = isP
  144. print('wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww',item)
  145. yield item