python扒取百宝彩网站江西*当日期号及开奖结果

时间:2022-04-26 05:34:43

一、环境

windows10+python27

二、需求:

1、获取百宝彩网站中,江西*当日的开奖期号和中奖号码;

2、根据输入期号,输出开奖号码;

三、上代码

 #!/bin/env python
 # coding=utf-8

 import urllib2
 import time
 import re

 class GetK3Issue:
     """
     获取百宝彩网站*当日输入的期号
     """
     def __init__(self, url=0, page=0, sys_time=0):
         """
         初始化参数
         """
         self.url = url
         self.page = page
         self.sys_time = sys_time

     def get_url_page(self):
         """
         获取页面返回元素
         """
         self.sys_time = time.strftime('%Y-%m-%d', time.localtime(time.time()))
         self.url = 'http://www.baibaocp.com/ks/history/lotid/10412/date/%s' % self.sys_time
         self.page = urllib2.urlopen(self.url).read().decode('utf8')
         return self.page

     def get_issue(self):
         """
         从页面返回元素中提取开奖期号
         """
         issue_list = []
         page = self.get_url_page()
         RE = re.compile(ur'<tr num=\'\d{9}')
         element_list = re.findall(RE, page)
         for element in element_list:
             issue = str(element[9:18])
             issue_list.append(issue)
         return list(issue_list)

     def get_winning_numbers(self):
         """
         从页面返回元素中获取开奖号码
         """
         numbers_list = []
         group_numbers_list = []
         page = self.get_url_page()
         RE = re.compile(ur"class='history_drawnumber'><span>.*</span>")
         element_numbers_list = re.findall(RE, page)
         list_a = str(element_numbers_list)
         RE2 = re.compile(ur'\d{1}</span>')
         numbers_str = re.findall(RE2, list_a)
         for dangezhi in numbers_str:
             numbers = dangezhi[0]
             numbers_list.append(numbers)

         i = 0
         while True:
             j = i + 3
             winning_numbers = numbers_list[i:j]
             i = j
             if j >= len(numbers_list)+3:
                 break
             group_numbers_list.append(winning_numbers)
         return group_numbers_list

     def result_dict(self):
         """
         合并期号和开奖号码列表,返回字典
         """
         issue = self.get_issue().__iter__()
         result = dict(zip(issue, self.get_winning_numbers()))
         return result

     def get_issue_by_dict(self):
         """
         通过输入期号查找开奖号码
         """
         input_issue = str(raw_input(u'请输入你想要查询的期号: '))
         result_list = self.result_dict()
         if input_issue in result_list:
             number = result_list[input_issue]
             print '今日第%s期开奖结果是:%s' % (input_issue, number)
         else:
             print '输入期号有误!!!'

 if __name__ == '__main__':
     run = GetK3Issue()
     run.get_issue_by_dict()

python扒取百宝彩网站江西*当日期号及开奖结果python扒取百宝彩网站江西*当日期号及开奖结果