def execute_sql(self, sql, database=None): # 激活连接 self.connection.ping() current_database = database if self.__database__: current_database = self.__database__ log_list = [ '————————数据库执行————————', 'Database: {}'.format(current_database), 'SQL:\n{}'.format(sql) ] if database: use_database_sql = 'USE {}'.format(database) self.cursor.execute(use_database_sql) rows = self.cursor.execute(sql) result = self.cursor.fetchall() self.connection.commit() log_list.append('执行结果数量: {}'.format(rows)) # log_list.append('执行结果\n{}'.format(result)) log_list.append('————————数据库执行结束————————') log_content = '\n'.join(log_list) log(log_content) return result
def print_tickets(self, times): ticket_list = [] for time in range(times): red_balls_copy = copy.copy(self.red_balls) blue_balls_copy = copy.copy(self.blue_balls) ticket = {} red_ball = [] blue_ball = [] for i in range(self.red_times): index = random.randint(0, len(red_balls_copy) - 1) rb = red_balls_copy.pop(index) red_ball.append(rb) for i in range(self.blue_times): index = random.randint(0, len(blue_balls_copy) - 1) bb = blue_balls_copy.pop(index) blue_ball.append(bb) red_ball.sort() blue_ball.sort() ticket['red_ball'] = red_ball ticket['blue_ball'] = blue_ball red_zone = ' '.join(map(str, red_ball)) blue_zone = ' '.join(map(str, blue_ball)) log(red_zone + ' | ' + blue_zone) ticket_list.append(ticket)
def lotto_image_source_api(draw_number): url = 'https://www.sporttery.cn/kj/lskj/3{draw_number}.html'.format(draw_number=draw_number) log(url) response = requests.get(url) content = response.content.decode('utf-8') return content
def lotto_pdf_api(draw_number): url = 'https://pdf.sporttery.cn/33800/{draw_number}/{draw_number}.pdf'.format(draw_number=draw_number) log(url) response = requests.get(url) content = response.content return content
def get_pdf_source_result(draw_number): pdf_content = open_api.lotto_pdf_api(draw_number) content_io = io.BytesIO(pdf_content) # log(pdf_content) with pdfplumber.open(content_io, password=b'') as pdf: page01 = pdf.pages[0] # 指定页码 text = page01.extract_text() # 提取文本 log(text) source_no = re.search('(?<=本期出球顺序: ).*?(?=\n)', text) log(source_no.group()) return source_no.group()
def match_rules(self, win_pro_zone_no, win_post_zone_no): log('前区正确个数: ' + str(win_pro_zone_no)) log('后区正确个数: ' + str(win_post_zone_no)) bonus = { '一等奖': '浮动', '二等奖': '浮动', '三等奖': '3000', '四等奖': '200', '五等奖': '10', '六等奖': '5', '未中奖': '0' } red_difference = self.pro_zone_times - win_pro_zone_no blue_difference = self.post_zone_times - win_post_zone_no # 红6蓝1 if red_difference == 0 and blue_difference == 0: level = '一等奖' # 红6蓝0 elif red_difference == 0 and blue_difference == 1: level = '二等奖' # 红5蓝1 elif red_difference == 1 and blue_difference == 0: level = '三等奖' # 红5蓝0 or 红4蓝1 elif (red_difference == 1 and blue_difference == 1)\ or (red_difference == 2 and blue_difference == 0): level = '四等奖' # 红4蓝0 or 红3蓝1 elif (red_difference == 2 and blue_difference == 1)\ or (red_difference == 3 and blue_difference == 0): level = '五等奖' # 红2蓝1 or 红1蓝1 or 红0蓝1 elif (red_difference == 4 and blue_difference == 0)\ or (red_difference == 5 and blue_difference == 0)\ or (red_difference == 6 and blue_difference == 0): level = '六等奖' else: level = '未中奖' return level, bonus[level]
def lotto_history_api(self): url = 'https://webapi.sporttery.cn/gateway/lottery/getHistoryPageListV1.qry' pre_payload = { 'gameNo': 85, 'provinceId': 0, 'pageSize': 30, 'isVerify': 1, 'pageNo': 1 } pre_response = requests.get(url=url, headers=self.default_headers, params=pre_payload) pre_response_obj = json.loads(pre_response.content) if pre_response.status_code == 200 and pre_response_obj['success']: total = pre_response_obj['value']['total'] else: raise Exception('Request Error') payload = { 'gameNo': 85, 'provinceId': 0, 'pageSize': total + 1, 'isVerify': 1, 'pageNo': 1 } log(payload) response = requests.get(url=url, headers=self.default_headers, params=payload) response_obj = json.loads(response.content) if response.status_code == 200 and response_obj['success']: return response_obj['value'] else: raise Exception('Request Error')
def print_ticket(self): red_balls_copy = copy.copy(self.red_balls) blue_balls_copy = copy.copy(self.blue_balls) ticket = {} red_ball = [] blue_ball = [] for i in range(self.red_times): index = random.randint(0, len(red_balls_copy) - 1) rb = red_balls_copy.pop(index) red_ball.append(rb) for i in range(self.blue_times): index = random.randint(0, len(blue_balls_copy) - 1) bb = blue_balls_copy.pop(index) blue_ball.append(bb) red_ball.sort() blue_ball.sort() ticket['red_ball'] = red_ball ticket['blue_ball'] = blue_ball log(ticket)
def check(self, tickets): self.win_pro_zone = self.win_ticket[:self.pro_zone_times] self.win_post_zone = self.win_ticket[-self.post_zone_times:] total_bonus = [] for ticket in tickets: ticket = ticket.split(' ') log(self.win_ticket) log(ticket) ticket_pro_zone = ticket[:self.pro_zone_times] ticket_post_zone = ticket[-self.post_zone_times:] win_pro_zone_no = self.check_pro_zone_no(ticket_pro_zone) win_post_zone_no = self.check_post_zone_no(ticket_post_zone) bonus = self.match_rules(win_pro_zone_no, win_post_zone_no) total_bonus.append(bonus[1]) log('共计中奖金额: ' + ', '.join(total_bonus))
import pandas_datareader as pdr import fix_yahoo_finance as yf from Core.Logger import log yf.pdr_override('yahoo') start_date = "2021-3-1" end_date = "2021-5-1" data = pdr.data.DataReader('600406.SZ', 'yahoo', start=start_date, end=end_date) log(data.describe)
return img_content def get_img_source_result(self, draw_number): image_content = self.get_img_content(draw_number) image_io = io.BytesIO(image_content) image_full = Image.open(image_io) # 裁剪截图成验证码图片 box = ( 300, 145, 680, 180 ) image_crop= image_full.crop(box) image_crop = image_crop.convert('L') # image_crop_prop.save('./test_crop1.png') config_str = '--psm 6 --oem 3 -c tessedit_char_whitelist=0123456789' result = pytesseract.image_to_string(image_crop, lang='eng', config=config_str).strip() return result if __name__ == '__main__': lotto_his = LottoHistory() result = lotto_his.get_pdf_source_result(21060) log(result)