一、环境
windows10+python27
二、需求:
1、获取百宝彩网站中,江西快三当日的开奖期号和中奖号码;
2、根据输入期号,输出开奖号码;
三、上代码
1 #!/bin/env python 2 # coding=utf-8 3 4 import urllib2 5 import time 6 import re 7 8 9 class GetK3Issue: 10 """ 11 获取百宝彩网站快三当日输入的期号 12 """ 13 def __init__(self, url=0, page=0, sys_time=0): 14 """ 15 初始化参数 16 """ 17 self.url = url 18 self.page = page 19 self.sys_time = sys_time 20 21 def get_url_page(self): 22 """ 23 获取页面返回元素 24 """ 25 self.sys_time = time.strftime(‘%Y-%m-%d‘, time.localtime(time.time())) 26 self.url = ‘http://www.baibaocp.com/ks/history/lotid/10412/date/%s‘ % self.sys_time 27 self.page = urllib2.urlopen(self.url).read().decode(‘utf8‘) 28 return self.page 29 30 def get_issue(self): 31 """ 32 从页面返回元素中提取开奖期号 33 """ 34 issue_list = [] 35 page = self.get_url_page() 36 RE = re.compile(ur‘<tr num=\‘\d{9}‘) 37 element_list = re.findall(RE, page) 38 for element in element_list: 39 issue = str(element[9:18]) 40 issue_list.append(issue) 41 return list(issue_list) 42 43 def get_winning_numbers(self): 44 """ 45 从页面返回元素中获取开奖号码 46 """ 47 numbers_list = [] 48 group_numbers_list = [] 49 page = self.get_url_page() 50 RE = re.compile(ur"class=‘history_drawnumber‘><span>.*</span>") 51 element_numbers_list = re.findall(RE, page) 52 list_a = str(element_numbers_list) 53 RE2 = re.compile(ur‘\d{1}</span>‘) 54 numbers_str = re.findall(RE2, list_a) 55 for dangezhi in numbers_str: 56 numbers = dangezhi[0] 57 numbers_list.append(numbers) 58 59 i = 0 60 while True: 61 j = i + 3 62 winning_numbers = numbers_list[i:j] 63 i = j 64 if j >= len(numbers_list)+3: 65 break 66 group_numbers_list.append(winning_numbers) 67 return group_numbers_list 68 69 def result_dict(self): 70 """ 71 合并期号和开奖号码列表,返回字典 72 """ 73 issue = self.get_issue().__iter__() 74 result = dict(zip(issue, self.get_winning_numbers())) 75 return result 76 77 def get_issue_by_dict(self): 78 """ 79 通过输入期号查找开奖号码 80 """ 81 input_issue = str(raw_input(u‘请输入你想要查询的期号: ‘)) 82 result_list = self.result_dict() 83 if input_issue in result_list: 84 number = result_list[input_issue] 85 print ‘今日第%s期开奖结果是:%s‘ % (input_issue, number) 86 else: 87 print ‘输入期号有误!!!‘ 88 89 if __name__ == ‘__main__‘: 90 run = GetK3Issue() 91 run.get_issue_by_dict()
时间: 2024-10-07 17:52:26