#!/usr/bin/env python # -*- coding: utf-8 -*- import datetime import jsonpath import redis __author__ = 'ethereal' __mtime__ = '2019/4/16' # -*- coding: utf-8 -*- import json import scrapy from scrapy.http import Request from ..items import Roll_Bangqiu class BqrangqiuSpider(scrapy.Spider): name = 'roll_bangqiu' to_day = datetime.datetime.now() allowed_domains = ['hg3535z.com'] custom_settings = { "ITEM_PIPELINES": { # "hg3535.pipeline.Roll_Banqiupipeline": 300, "hg3535.pipeline.roll_bangqiu.Roll_Banqiupipeline": 350, }, # 'LOG_LEVEL': 'DEBUG', # 'LOG_FILE': "../hg3535/log/roll_bangqiu_{}_{}_{}.log".format(to_day.year, to_day.month, to_day.day) } # start_urls = ['https://hg3535z.com/odds2/d/getodds?sid=4&pt=4&ubt=am&pn=0&sb=2&dc=null&pid=0'] # 滚球菜单 棒球滚球列url start_urls = [ 'https://odata.jiushan6688.com/odds6i/d/getodds/zh-cn/sid/4/pt/4/ubt/am/pn/0/sb/2/dc/null/pid/0'] # 滚球菜单 棒球滚球列url rls = redis.Redis(host='192.168.2.200', port=6379, db=1, password=123456) def parse(self, response): datas = json.loads(response.text) ids = jsonpath.jsonpath(datas, '$..i-ot[0]..egs..es..i[16]') # ids新列表 if ids: for i in ids: urls = 'https://odata.jiushan6688.com/odds6i/d/getamodds/zh-cn/eid/{}/iip/true/ubt/am/isp/false'.format( i) yield Request(url=urls, callback=self.parse_each, dont_filter=True) def parse_each(self, response): datas = json.loads(response.text) # 倒三角形 sa向下,sh向上 triangle = datas['i'][5] try: new_datas = datas['eg'] except: new_datas = "" try: pt = response.meta['pt'] except: pt = 0 if new_datas: # 联赛id league_id = new_datas["c"]["k"] # 联赛名 league_name = new_datas["c"]["n"] new_data = new_datas["es"] for result in new_data: if result['pci']["ctid"] == 0: item = Roll_Bangqiu() # 比赛id game_id = str(result['k']) # 球队1 team_home = result['i'][0] # 球队2 team_guest = result['i'][1] # 数量(97>) number = result['i'][2] # 比赛状态 zhuangtai = result['i'][3] # 日期 data_game = result['i'][4] # 开赛时间 time_game = result['i'][5] # 队1分数 score_home1 = result['i'][10] # 队2分数 score_guest1 = result['i'][11] # 第几盘 # jijie = result['i'][12] # 球队得分 qiudui = result['pci'].get('ctn', "") # 是否出局 不一定存在 ot = result['sb'].get("ot", "") ps_list = result['sb']['ps'] # 右菱形bool值 fb = result['sb']['fb'] # 左菱形bool值 tb = result['sb']['tb'] # 上菱形bool值 sb = result['sb']['tb'] # 局数或者节或者分 cp = result['sb']['cp'] # 第一个队伍 a = result['sb']['a'] # 第二个队伍 h = result['sb']['h'] d = {} d['ps'] = ps_list d['cp'] = cp d['ot'] = ot d['triangle'] = triangle d['fb'] = fb d['tb'] = tb d['sb'] = sb # --------------------------------------------------让球分割线---------------------------------------- concedes_dict = {} concedes_dict_rule = {} try: concedes = result['o']["ah"]["v"] concedes_dict_rule['coh'] = concedes[1] concedes_dict['coh'] = concedes[5] concedes_dict_rule['cog'] = concedes[3] concedes_dict['cog'] = concedes[7] except: concedes_dict_rule['coh'] = "" concedes_dict['coh'] = "" concedes_dict_rule['cog'] = "" concedes_dict['cog'] = "" # 让球 前5局------------------------------------------------------------------------------------------ try: concedes = result['o']["ahf5in"]["v"] concedes_dict_rule['coh_5'] = concedes[1] concedes_dict['coh_5'] = concedes[5] concedes_dict_rule['cog_5'] = concedes[3] concedes_dict['cog_5'] = concedes[7] except: concedes_dict_rule['coh_5'] = "" concedes_dict['coh_5'] = "" concedes_dict_rule['cog_5'] = "" concedes_dict['cog_5'] = "" # ---------------------------------------------------独赢-------------------------------------------- capots_dict = {} try: capots = result['o']["ml"]["v"] capots_dict['ch'] = capots[1] capots_dict['cg'] = capots[3] except: capots_dict['ch'] = "" capots_dict['cg'] = "" # ---------------------------------------------------总得分:大/小------------------------------------- total_size_dict = {} total_size_dict_rule = {} try: total_sizes = result['o']["ou"]["v"] total_size_dict_rule['tnb'] = total_sizes[1] total_size_dict['tnb'] = total_sizes[5] total_size_dict_rule['tns'] = total_sizes[3] total_size_dict['tns'] = total_sizes[7] except: total_size_dict_rule['tnb'] = "" total_size_dict['tnb'] = "" total_size_dict_rule['tns'] = "" total_size_dict['tns'] = "" # 总得分: 大/小 前5局---------------------------------------------------------------------------------- try: total_sizes = result['o']["ouf5in"]["v"] total_size_dict_rule['tnb_5'] = total_sizes[1] total_size_dict['tnb_5'] = total_sizes[5] total_size_dict_rule['tns_5'] = total_sizes[3] total_size_dict['tns_5'] = total_sizes[7] except: total_size_dict_rule['tnb_5'] = "" total_size_dict['tnb_5'] = "" total_size_dict_rule['tns_5'] = "" total_size_dict['tns_5'] = "" # ------------------------------------------------------总得分:单/双---------------------------------- odd_evens_dict = {} odd_evens_dict_rule = {} try: odd_evens = result['o']["oe"]["v"] # 上半场 主队让球条件 odd_evens_dict['tss'] = odd_evens[1] odd_evens_dict_rule['tss'] = "单" odd_evens_dict['tsd'] = odd_evens[3] odd_evens_dict_rule['tsd'] = "双" except: odd_evens_dict['tss'] = "" odd_evens_dict['tsd'] = "" odd_evens_dict_rule['tss'] = "" odd_evens_dict_rule['tsd'] = "" team_source_dict = {} try: team_sources = result['sb']['ps'] for team_source in team_sources: if team_source['p'] == 'i1': team_source_dict['one_score'] = str(team_source['h']) + '-' + str(team_source['a']) if team_source['p'] == 'i2': team_source_dict['two_score'] = str(team_source['h']) + '-' + str(team_source['a']) if team_source['p'] == 'i3': team_source_dict['three_score'] = str(team_source['h']) + '-' + str(team_source['a']) if team_source['p'] == 'i4': team_source_dict['four_score'] = str(team_source['h']) + '-' + str(team_source['a']) if team_source['p'] == 'i5': team_source_dict['five_score'] = str(team_source['h']) + '-' + str(team_source['a']) if team_source['p'] == 'i6': team_source_dict['six_score'] = str(team_source['h']) + '-' + str(team_source['a']) if team_source['p'] == 'i7': team_source_dict['seven_score'] = str(team_source['h']) + '-' + str(team_source['a']) if team_source['p'] == 'i8': team_source_dict['eight_score'] = str(team_source['h']) + '-' + str(team_source['a']) if team_source['p'] == 'i9': team_source_dict['nine_score'] = str(team_source['h']) + '-' + str(team_source['a']) # 加时 if team_source['p'] == 'ot': team_source_dict['overtime_score'] = str(team_source['h']) + '-' + str(team_source['a']) # 总局数比分 if team_source['p'] == 'ft': team_source_dict['total_inning'] = int(team_source['h']) + int(team_source['a']) team_source_dict['total_source'] = str(team_source['h']) + '-' + str(team_source['a']) except: team_source_dict['one_score'] = '' team_source_dict['two_score'] = '' team_source_dict['three_score'] = '' team_source_dict['four_score'] = '' team_source_dict['five_score'] = '' team_source_dict['six_score'] = '' team_source_dict['seven_score'] = '' team_source_dict['eight_score'] = '' team_source_dict['nine_score'] = '' team_source_dict['overtime_score'] = '' team_source_dict['total_inning'] = 0 team_source_dict['total_source'] = '' # 联赛id item['league_id'] = league_id # 联赛名 item['league_name'] = league_name # 比赛id item['game_id'] = game_id # 球队1 item['team_home'] = team_home # 球队2 item['team_guest'] = team_guest # 数量(97>) item['number'] = number # 比赛状态 item['zhuangtai'] = zhuangtai # 日期 item['data_game'] = data_game # 开赛时间 item['time_game'] = time_game # 队1分数 item['score_home'] = score_home1 # 队2分数 item['score_guest'] = score_guest1 # 第几节 item['jijie'] = cp item['pt'] = pt # 球队得分 item['qiudui'] = "{}:{}".format(a, h) # 让球 item['concedes_dict'] = concedes_dict item['concedes_dict_rule'] = concedes_dict_rule # 独赢 item['capots_dict'] = capots_dict # 总得分:大/小 item['total_size_dict'] = total_size_dict item['total_size_dict_rule'] = total_size_dict_rule # 总得分:单/双 item['odd_evens_dict'] = odd_evens_dict item['odd_evens_dict_rule'] = odd_evens_dict_rule item['match_score_dict'] = d item['team_source_dict'] = team_source_dict yield item