From d67e21c0333af777d227bd3ede9a358346771071 Mon Sep 17 00:00:00 2001 From: chenwj113 Date: Thu, 11 May 2023 18:14:50 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=A2=9E=E5=8A=A0=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E8=83=9C=E8=B4=9F=E5=BD=A9=E6=95=B0=E6=8D=AE=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- get_data.py | 102 +++++++++++++----- lottery/__init__.py | 28 ++++- lottery/football/__init__.py | 19 ++++ .../football}/get_football_data.py | 0 lottery/football/get_game14.py | 64 +++++++++++ {football => lottery/football}/jcbf.py | 0 lottery/pls.py | 6 +- model/football_sfc.py | 26 +++++ model/sd.py | 4 +- tasks/cron_tasks.py | 8 +- youdian.py | 4 +- 11 files changed, 222 insertions(+), 39 deletions(-) create mode 100644 lottery/football/__init__.py rename {football => lottery/football}/get_football_data.py (100%) create mode 100644 lottery/football/get_game14.py rename {football => lottery/football}/jcbf.py (100%) create mode 100644 model/football_sfc.py diff --git a/get_data.py b/get_data.py index a53e21e..b4a612c 100644 --- a/get_data.py +++ b/get_data.py @@ -1,7 +1,6 @@ from requests_html import HTML, HTMLSession from retrying import retry - from lottery import Lottery session = HTMLSession() @@ -29,29 +28,84 @@ def get_data(url, lottery_type): print('异常出错重试后,依然报错') raise e -def main(basic_url, lottery_type): - """爬取相关数据""" - r = session.get(basic_url) - select_list = list(reversed(r.html.find('div.kjxq_box02_title_right span div a'))) - for item in select_list: - html = HTML(html=item.html) - url = html.find('a', first=True).attrs['href'] - try: - get_data(url, lottery_type) + + +class DataGetter(object): + + def __init__(self): + self.session = HTMLSession() + + def _get_data(self, issue, lottery_type='pls'): + """ + """ + r = self.session.get(f"https://kaijiang.500.com/shtml/{lottery_type}/{issue}.shtml") + if lottery_type == 'sfc': + # 开奖期数 + issue_elem = r.html.search('胜负彩(任九) 第 {} 期') + draw_issue = HTML(html=issue_elem[0]).text + # 胜负彩销量 + sfc_sale_elem = r.html.search('本期胜负彩销量:{}元') + sfc_sale = HTML(html=sfc_sale_elem[0]).text.replace(",", "") + # 任九销量 + rj_sale_elem = r.html.search('本期任九销量:{}元') + rj_sale = HTML(html=rj_sale_elem[0]).text.replace(",", "") + # 开奖结果 + table_list = r.html.find("table.kj_tablelist02", first=True) + draw_code = table_list.find("tr")[2].text.replace('\n', ' ') + # 开奖日期 + draw_date = table_list.find("td.td_title01 span.span_right")[0].text + # 中奖详情 + table_list_02 = r.html.find("table.kj_tablelist02")[1] + award_1_elem = table_list_02.find("tr")[2] + award_2_elem = table_list_02.find("tr")[3] + rj_award_elem = table_list_02.find("tr")[4] + award_1_num = award_1_elem.find("td")[1].text.replace(",", "") + award_1_money = award_1_elem.find("td")[2].text.replace(",", "") + award_2_num = award_2_elem.find("td")[1].text.replace(",", "") + award_2_money = award_2_elem.find("td")[2].text.replace(",", "") + r9_award_num = rj_award_elem.find("td")[1].text.replace(",", "") + r9_award_money = rj_award_elem.find("td")[2].text.replace(",", "") + with Lottery(lottery_type='sfc') as lottery: + last_id = lottery.insert(draw_issue, draw_date, draw_code, sfc_sale=sfc_sale, rj_sale=rj_sale, + award_1_num=award_1_num, award_1_money=award_1_money, award_2_num=award_2_num, award_2_money=award_2_money, r9_award_num=r9_award_num, r9_award_money=r9_award_money) + if last_id: + print(f"issue:{draw_issue}数据写入完成。。。") + else: + print(f'issue:{draw_issue}已经存在') + else: + + table_list = r.html.find("table.kj_tablelist02", first=True) + draw_issue = table_list.find("td.td_title01 span.span_left strong")[0].text + draw_date = table_list.find("td.td_title01 span.span_right")[0].text + draw_code = table_list.find("div.ball_box01")[0].text.replace('\n', '') + with Lottery(lottery_type=lottery_type) as lottery: + last_id = lottery.insert(draw_issue, draw_date, draw_code) + if last_id: + print(f"issue:{draw_issue}数据写入完成。。。") + else: + print(f'issue:{draw_issue}已经存在') + + def pls(self, issue): + return self._get_data(issue, lottery_type='pls') + + def plw(self, issue): + return self._get_data(issue, lottery_type='plw') + + def sd(self, issue): + return self._get_data(issue, lottery_type='sd') + + def sfc(self, issue): + return self._get_data(issue, lottery_type='sfc') + +if __name__ == '__main__': + # issue = "23108" + # url = f"https://kaijiang.500.com/shtml/pls/{issue}.shtml" + # get_data(url, lottery_type='pls') + getter = DataGetter() + for i in range(1, 64): + try : + issue = f"{23}{i:03d}" + getter.sfc(issue=issue) except Exception as e: print(e) continue - -if __name__ == '__main__': - issue = "23108" - url = f"https://kaijiang.500.com/shtml/pls/{issue}.shtml" - get_data(url, lottery_type='pls') - # for y in range(23, 24): - # for i in range(1, 365): - # try : - # issue = f"{y:02d}{i:03d}" - # url = f"https://kaijiang.500.com/shtml/plw/{issue}.shtml" - # get_data(url, lottery_type='plw') - # except Exception as e: - # print(e) - # continue \ No newline at end of file diff --git a/lottery/__init__.py b/lottery/__init__.py index 2479e7b..37808eb 100644 --- a/lottery/__init__.py +++ b/lottery/__init__.py @@ -7,6 +7,7 @@ from model.pls import PLS from model.plw import PLW from model.sd import SD from model.klb import KLB +from model.football_sfc import FootballSfc class Lottery(object): """ @@ -23,6 +24,8 @@ class Lottery(object): self._Model = SD elif lottery_type.lower() == 'plw': self._Model = PLW + elif lottery_type.lower() == 'sfc': + self._Model = FootballSfc elif lottery_type.lower() == 'klb': self._Model = KLB else: @@ -37,9 +40,14 @@ class Lottery(object): return self def __exit__(self, type, value, trace): + print("close the db session...") self.db.close() + def insert(self, draw_issue, draw_date, draw_code, **kwargs): + """ + 插入3d、排列三、排列五 + """ result = self.db.query(self._Model).filter_by(draw_issue=draw_issue).first() if result is None: record = self._Model() @@ -55,15 +63,14 @@ class Lottery(object): record.draw_date = date(int(m2.group(1)), int(m2.group(2)), int(m2.group(3))) else: raise Exception(f"issue:{draw_issue}数据写入失败。。。") - # 如果是排列3和3D - if isinstance(record, PLS) or isinstance(record, SD) or isinstance(record, PLW): - record.draw_code = draw_code + # 如果是排列五、排列五和3D + if isinstance(record, (PLS, SD, PLW)): record.sum_num = sum(map(int, draw_code)) record.code_small = len(list(filter(lambda x: True if int(x) < 5 else False, draw_code))) record.code_big = len(list(filter(lambda x: True if int(x) >= 5 else False, draw_code))) record.code_single = len(list(filter(lambda x: True if int(x) % 2 == 1 else False, draw_code))) record.code_double = len(list(filter(lambda x: True if int(x) % 2 == 0 else False, draw_code))) - if isinstance(record, PLS) or isinstance(record, SD): + if isinstance(record, (PLS, SD)): record.hundred = draw_code[0] record.ten = draw_code[1] record.one = draw_code[2] @@ -85,8 +92,19 @@ class Lottery(object): record.sum_hundred_one = int(draw_code[2]) + int(draw_code[4]) record.sum_hundred_ten = int(draw_code[2]) + int(record.draw_code[3]) record.sum_ten_one = int(record.draw_code[3]) + int(record.draw_code[4]) + elif isinstance(record, FootballSfc): + record.open_result = draw_code + record.sale_money = kwargs["sfc_sale"] + record.r9_sale_money = kwargs["rj_sale"] + record.award_1_money = kwargs["award_1_money"] + record.award_1_num = kwargs["award_1_num"] + record.award_2_money = kwargs["award_2_money"] + record.award_2_num = kwargs["award_2_num"] + record.r9_award_money = kwargs["r9_award_money"] + record.r9_award_num = kwargs["r9_award_num"] self.db.add(record) self.db.commit() return record.id else: - return None \ No newline at end of file + print(f"{draw_issue}开奖结果已经写入数据库中...") + return None diff --git a/lottery/football/__init__.py b/lottery/football/__init__.py new file mode 100644 index 0000000..0a15a25 --- /dev/null +++ b/lottery/football/__init__.py @@ -0,0 +1,19 @@ + +from sqlalchemy import create_engine +from sqlalchemy.orm import sessionmaker + + +class Foootball(object): + def __init__(self): + self.db = self._get_db_session() + + def _get_db_session(self): + _engine = create_engine("mysql+pymysql://test:123456@localhost/lottery?charset=utf8", pool_pre_ping=True, pool_recycle=3600) + _DbSession = sessionmaker(bind=_engine) + return _DbSession() + + def __enter__(self): + return self + + def __exit__(self, type, value, trace): + self.db.close() \ No newline at end of file diff --git a/football/get_football_data.py b/lottery/football/get_football_data.py similarity index 100% rename from football/get_football_data.py rename to lottery/football/get_football_data.py diff --git a/lottery/football/get_game14.py b/lottery/football/get_game14.py new file mode 100644 index 0000000..a60d85e --- /dev/null +++ b/lottery/football/get_game14.py @@ -0,0 +1,64 @@ +import arrow +import requests_html +import furl +import time + +class Game14(object): + _open_url = "https://webapi.sporttery.cn/gateway/lottery/getFootBallDrawInfoByDrawNumV1.qry" + + def __init__(self, issue, lottery_type="sfc14c"): + self._issue = issue + self._lottery_type = lottery_type + self._now = arrow.now().format("YYYY-MM-DD HH:mm:ss") + self._session = requests_html.HTMLSession() + self._data = self._get_data() + + def _get_data(self): + # d1 = dict(v=int(time.time()*1000), lotteryType=self._lottery_type, issue=self._issue) + # print(d1) + d = dict(isVerify=1, lotteryGameNum=90, lotteryDrawNum=self._issue) + r = self._session.get(furl.furl(self._open_url).add(d)) + print(r.json()) + # match_list_1 = r1.json()["result"]["matchList"] + match_list = r.json()["value"]["matchList"] + data = list() + for idx, m in enumerate(match_list): + m["finalScore"] = match_list_2[idx]["czScore"] + m["result"] = match_list_2[idx]["result"] + data.append(m) + return data + + def get_odds(self): + odds = list() + for m in self._data: + o = dict() + o["issue"] = m["issue"] + o["match_no"] = m["matchId"] + o["fix_id"] = m["dataXiUrl"] + o["match_datetime"] = "20{0}-{1}".format(m["issue"][:2], m["matchTime"]) + o["host_name"] = m["homeTeam"] + o["away_name"] = m["awaryTeam"] + o["score"] = m["finalScore"] or "-:-" + o["result"] = m["result"].replace("*", "-") + o["win"] = float(m["winRate"]) + o["draw"] = float(m["deuceRate"]) + o["lost"] = float(m["loseRate"]) + o["create_time"] = self._now + odds.append(o) + return odds + + def print_data(self): + fix_ids = [] + for i in self._data: + fix_ids.append(i["dataXiUrl"]) + print(fix_ids) + +def main(): + obj = AicaiGame14("23060") + obj.print_data() + odds = obj.get_odds() + for i in odds: + print(i) + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/football/jcbf.py b/lottery/football/jcbf.py similarity index 100% rename from football/jcbf.py rename to lottery/football/jcbf.py diff --git a/lottery/pls.py b/lottery/pls.py index af853b2..ad6abba 100644 --- a/lottery/pls.py +++ b/lottery/pls.py @@ -44,10 +44,10 @@ for i in sum_group6_dict.keys(): f1 = lambda number: len([i for i in number if int(i) %2 == 0]) != 0 f2 = lambda number: len([i for i in number if int(i) %2 == 1]) != 0 f3 = lambda n: True if int(n[1]) - int(n[0]) == 1 or int(n[2]) - int(n[1]) == 1 else False - f4 = lambda number: True if '3' not in number else False - f5 = lambda number: True if '0' in number or '3' in number else False + f4 = lambda number: True if '4' not in number and '6' not in number else False + f5 = lambda number: True if '2' in number or '5' in number or '7' in number else False f6 = lambda number: True if len(set('279').difference(number)) == 2 else False - if i in [13, 14, 15, 16, 17]: + if i in [12, 13, 14, 15, 16, 17]: # result = sum_group6_dict[i] + sum_group3_dict[i] result = sum_group3_dict[i] print("-"*15 + "组三" + "-"*15) diff --git a/model/football_sfc.py b/model/football_sfc.py new file mode 100644 index 0000000..79f4f2f --- /dev/null +++ b/model/football_sfc.py @@ -0,0 +1,26 @@ +# coding: utf-8 +from sqlalchemy import Column, DateTime, Float, Integer, text +from sqlalchemy.dialects.mysql import VARCHAR +from sqlalchemy.ext.declarative import declarative_base + +Base = declarative_base() +metadata = Base.metadata + + +class FootballSfc(Base): + __tablename__ = 'football_sfc_2023' + __table_args__ = {'comment': '足彩胜负彩/任选九表'} + + id = Column(Integer, primary_key=True) + draw_issue = Column(VARCHAR(10), comment='期数') + draw_date = Column(DateTime, comment='开奖时间') + draw_code = Column(VARCHAR(50), comment='开奖结果') + sale_money = Column(Integer, comment='胜负彩销售额') + award_1_money = Column(Float(10), comment='一等奖单注奖金') + award_1_num = Column(Integer, comment='一等奖中奖数') + award_2_money = Column(Float(10), comment='二等奖单注奖金') + award_2_num = Column(Integer, comment='二等奖中奖数') + r9_sale_money = Column(Integer, comment='任九销售额') + r9_award_money = Column(Float(10), comment='任九奖金') + r9_award_num = Column(Integer, comment='任九中奖数') + created_at = Column(DateTime, server_default=text("CURRENT_TIMESTAMP")) diff --git a/model/sd.py b/model/sd.py index 17a61dc..1d9e035 100644 --- a/model/sd.py +++ b/model/sd.py @@ -1,5 +1,5 @@ # coding: utf-8 -from sqlalchemy import Column, Date, Integer +from sqlalchemy import Column, Date, DateTime, Integer from sqlalchemy.dialects.mysql import VARCHAR from sqlalchemy.ext.declarative import declarative_base @@ -9,6 +9,7 @@ metadata = Base.metadata class SD(Base): __tablename__ = 'sd' + __table_args__ = {'comment': '福彩3d统计表'} id = Column(Integer, primary_key=True) draw_issue = Column(VARCHAR(25)) @@ -28,3 +29,4 @@ class SD(Base): sum_hundred_one = Column(Integer, comment='百位各位和') sum_single_double = Column(Integer, comment='总和奇偶') sum_big_small = Column(Integer, comment='总和大小') + created_at = Column(DateTime) diff --git a/tasks/cron_tasks.py b/tasks/cron_tasks.py index df088f3..63f54b2 100644 --- a/tasks/cron_tasks.py +++ b/tasks/cron_tasks.py @@ -37,18 +37,18 @@ def get_data_job(lottery_type='pls'): print(f'issue:{draw_issue}已经存在') -@repeat(every().day.at("00:01")) +@repeat(every().day.at("18:01")) def pls_job(): get_data_job(lottery_type='pls') -@repeat(every().day.at("00:02")) +@repeat(every().day.at("18:02")) def plw_job(): get_data_job(lottery_type='plw') -@repeat(every().day.at("00:03")) +@repeat(every().day.at("18:03")) def sd_job(): get_data_job(lottery_type='sd') while True: run_pending() - time.sleep(1) \ No newline at end of file + time.sleep(1) diff --git a/youdian.py b/youdian.py index e3fe861..7ff721f 100644 --- a/youdian.py +++ b/youdian.py @@ -259,8 +259,8 @@ class Chrome(object): with Chrome() as chrome: # chrome.balance() # 组选 - result = ['059', '158', '167', '058', '067', '049', '256', '247', '148', '458', '269', '089', '278', '467', '069', '168', '249', '267', '258', '078'] - chrome.pls(result = result, multiple=2) + result = ['058', '157', '238', '139', '278', '359', '089', '179', '138', '057'] + chrome.pls(result = result, multiple=1) # 直选 # result = ['246', '264', '291', '219', '169'] # chrome.pls(result=result, is_group=False) \ No newline at end of file