import sys sys.path.append("/root/documents/py_scripts") from furl import furl from requests_html import HTML, HTMLSession import pandas as pd from football import Foootball from model.football_match import FootballMatch from model.football_match_spf_odds import FootballMatchSpfOdd from model.football_match_bf_odds import FootballMatchBfOdd from model.football_match_bqc_odds import FootballMatchBqcOdd from model.football_match_zjq_odds import FootballMatchZjqOdd class Jczq(Foootball): ''' 竞彩足球赔率获取 ''' _base_url = furl("https://trade.500.com/jczq/?g=2&playid=312") def __init__(self, date_str): super().__init__() self.session = HTMLSession() self._base_url.args["date"] = date_str def get_odds(self): # print(self._base_url.url) r = self.session.get(self._base_url) bet_trs = r.html.find("tr.bet-tb-tr") bet_more_trs = r.html.find("tr.bet-more-wrap") for tr in zip(bet_trs, bet_more_trs): # 对应竞彩网matchId match = self.db.query(FootballMatch).filter_by(issue=tr[0].attrs["data-id"]).first() if match is not None: continue match = FootballMatch() match.issue = tr[0].attrs["data-id"] match_num = tr[0].attrs["data-matchnum"] match.league_name = tr[0].attrs["data-simpleleague"] match.home_name = tr[0].attrs["data-homesxname"] match.away_name = tr[0].attrs["data-awaysxname"] match.match_date = tr[0].attrs["data-matchdate"] + " " + tr[0].attrs["data-matchtime"] match.buy_end_time = tr[0].attrs["data-buyendtime"] match.rq_count = tr[0].attrs["data-rangqiu"] match.score = tr[0].find("i.team-bf", first=True).text self.db.add(match) self.db.commit() match_id = match.id match_issue = match.issue # 胜平负 nspf = tr[0].find("td.td-betbtn div.itm-rangB1", first=True) betbtn_ok = nspf.find("p[class$='betbtn-ok']", first=True) # 比分 if betbtn_ok is not None: result = betbtn_ok.attrs["data-value"] nspf_odds = {i.attrs["data-value"]: i.attrs["data-sp"] for i in nspf.find("p.betbtn")} # 让球胜平负 spf = tr[0].find("td.td-betbtn div.itm-rangB2", first=True) rq_result = spf.find("p[class$='betbtn-ok']", first=True).attrs["data-value"] spf_odds = {i.attrs["data-value"]: i.attrs["data-sp"] for i in spf.find("p.betbtn")} spf_model = FootballMatchSpfOdd(match_id=match_id, issue=match_issue, rq_count=match.rq_count, rq_result=rq_result) spf_model.win = nspf_odds["3"] spf_model.draw = nspf_odds["1"] spf_model.lost = nspf_odds["0"] spf_model.rq_win = spf_odds["3"] spf_model.rq_draw = spf_odds["1"] spf_model.rq_lost = spf_odds["0"] self.db.add(spf_model) # 半全场 bqc = tr[1].find("p[data-type='bqc']") bqc_odds = {"_" + i.attrs["data-value"].replace("-", "_"): i.attrs["data-sp"] for i in bqc} bqc_result = tr[1].find("p[class$='sbetbtn-ok'][data-type='bqc']", first=True).attrs["data-value"] bqc_model = FootballMatchBqcOdd(match_id=match_id, issue=match_issue, result=bqc_result, **bqc_odds) self.db.add(bqc_model) # 比分 bf = tr[1].find("p[data-type='bf']") bf_odds = {i.attrs["data-value"]: i.attrs["data-sp"] for i in bf} bf_odds = {("_" + k.replace(":", "_")).replace("_胜其它", "win_others").replace("_平其它", "draw_others").replace("_负其它", "lost_others"): bf_odds[k] for k in bf_odds.keys()} bf_result = tr[1].find("p[class$='sbetbtn-ok'][data-type='bf']", first=True).attrs["data-value"] bf_model = FootballMatchBfOdd(match_id=match_id, issue=match_issue, result=bf_result, **bf_odds) self.db.add(bf_model) # 进球数 jqs = tr[1].find("p[data-type='jqs']") jqs_odds = {"ball_" + i.attrs["data-value"].replace("7", "7_plus"): i.attrs["data-sp"] for i in jqs} jqs_result = tr[1].find("p[class$='sbetbtn-ok'][data-type='jqs']", first=True).attrs["data-value"] zjq_model = FootballMatchZjqOdd(match_id=match_id, issue=match_issue, result=jqs_result, **jqs_odds) self.db.add(zjq_model) self.db.commit() if __name__ == "__main__": # dates = pd.date_range(start='2023-04-15', end='2023-05-13') # print(dates) jczq = Jczq("2023-04-15") jczq.get_odds()