目录
  • 1. 随机一注
  • 2. 红球固定或蓝球固定
    • 红球固定
    • 蓝球固定
  • 3. 爬取中奖号码
    • 4. 是否中奖
      • 5. 总结一下

        大家好,我是煎蛋哥!

        上篇文章聊到了 Python 实现大乐透彩票自由的完整流程

        如何使用 Python 实现彩票自由(大乐透)

        和体彩大乐透类似,福彩双色球也是购买次数最多的彩种之一,相比大乐透,双色球更容易中小奖

        下面将介绍 Python 实习双色球彩票自由的流程

        1. 随机一注

        福彩双色球一注同样包含 7 个数字,包含 6 个红球和 1 个篮球

        其中

        • 红球是从 1 – 33 中选择 6 个不同的数字
        • 蓝球是从 1 – 16 中选择 1 个不同的数字

        使用 Python 随机生成一注双色球号码,部分代码如下:

        def gene_ssq(number):
            """
            随机产生几注双色球(6+1)
            :param number:
            :return:
            """
            result = []
        
            for item in range(number):
                reds = []
        
                # 产生6个红球
                while len(reds) < 6:
                    # 从1-33中随机取一个数字
                    temp_red_num = random.randint(1, 33)
                    if temp_red_num not in reds:
                        reds.append(temp_red_num)
        
                # 蓝球
                blue = random.randint(1, 16)
        
                # 红球排序
                reds.sort()
        
                # 数据预处理
                reds = nums_pre(reds)
                blue = nums_pre([blue])[0]
        
                result.append(' '.join(reds) + " + " + blue)
            return '\n'.join(result)
        

        需要注意的是,为了方便后面判断是否中奖,这里对红球列表进行了一次数据预处理,将小于 10 的数字前面加上 0

        def nums_pre(nums):
            """
            购买数字预处理,如果是个位数,加上0
            :param nums:
            :return:
            """
            if nums:
                if isinstance(nums, list) or isinstance(nums,tuple):
                    return ['0{}'.format(int(item)) if int(item) < 10 else str(int(item)) for item in nums]
                else:
                    return '0{}'.format(int(nums)) if int(nums) < 10 else str(int(nums))
            else:
                return ''

        2. 红球固定或蓝球固定

        这里以红球固定、蓝球固定两个最简单的场景为例,其他复杂的场景可以自行拓展

        红球固定

        红球固定的情况下,我们只需要随机生成一个蓝球,然后进行数据预处理,最后组成一注号码即可

        def gene_blue_random_ssq(reds, number):
            """
            红球固定,蓝球随机
            :param reds:
            :param number:
            :return:
            """
            result = []
        
            for item in range(number):
                # 蓝球
                blue = random.randint(1, 16)
        
                # 红球排序
                reds.sort()
        
                # 数据预处理
                reds = nums_pre(reds)
                blue = nums_pre([blue])[0]
        
                result.append(' '.join(reds) + " + " + blue)
            return '\n'.join(result)

        蓝球固定

        蓝球固定时,我们只需要从 1-33 中随机生成 6 个不同的数字组成红球

        def gene_red_random_ssq(blue, number):
            """
            蓝球固定,红球随机
            :param blue:
            :param number:
            :return:
            """
            result = []
        
            for item in range(number):
                reds = []
        
                # 产生6个红球
                while len(reds) < 6:
                    # 从1-33中随机取一个数字
                    temp_red_num = random.randint(1, 33)
                    if temp_red_num not in reds:
                        reds.append(temp_red_num)
        
                # 红球排序
                reds.sort()
        
                # 数据预处理
                reds = nums_pre(reds)
                blue = nums_pre([blue])[0]
        
                result.append(' '.join(reds) + " + " + blue)
            return '\n'.join(result)

        3. 爬取中奖号码

        相比体彩大乐透,双色球的开奖时间会稍微一些,煎蛋哥建议选择晚上 10 点半进行爬虫

        目标地址:

        aHR0cDovL2thaWppYW5nLjUwMC5jb20vc3RhdGljL2luZm8va2FpamlhbmcveG1sL3NzcS9saXN0LnhtbA==

        该网站通过 XML 数据展示了过去每一期双色球的中奖号码,我们只需要使用正则表达式匹配出所有中奖号码,取最近的一期号码即可

        import re
        import requests
        
        class SSQ(object):
        
            def __init__(self):
                # 具体的地址请解码后自行替换
                self.url = '**/xml/ssq/list.xml'
                self.headers = {
                    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36'
                }
        
            def get_last_ssq_lucky(self):
                # 发起请求
                reponse = requests.get(url=self.url, headers=self.headers)
        
                # 正则规则
                pattern = re.compile(r'<row.*?expect="(.*?)".*?opencode="(.*?)".*?opentime="(.*?)"')
        
                # 双色球数据
                ssq_raw_list = pattern.findall(reponse.text)
        
                results = []
        
                for item in ssq_raw_list:
                    # 期数、数据、时间
                    no, info, create_at = item
                    # 6个红球、1个篮球
                    red, blue = info.split("|")
        
                    red_datas = red.split(",")
        
                    results.append(
                        [no, red_datas[0], red_datas[1], red_datas[2], red_datas[3], red_datas[4], red_datas[5], blue,
                         create_at]
                    )
        
                # 最近的一期中奖号码
                last_lottery = results[0]
        
                return [last_lottery[1], last_lottery[2], last_lottery[3], last_lottery[4], last_lottery[5], last_lottery[6]], \
        
                       last_lottery[7]
        

        4. 是否中奖

        根据双色球官网提供中奖规则,我们根据红球中奖个数、蓝球中奖个数组成中奖信息即可

        实现代码如下:

        ...
        def judge_ssq_lucky(red_nums_result, red_nums_buy, blue_num_result, blue_num_buy):
            """
            根据中奖号码及购买号码,返回对应的中奖信息
            :param red_nums_result:
            :param red_nums_buy:
            :param blue_num_result:
            :param blue_num_buy:
            :return:
            """
            # 红球预测的数目
            red_lucky_count = 0
            # 篮球预测的数目
            blue_lucky_count = 0
        
            # 数据预处理
            red_nums_buy = nums_pre(red_nums_buy)
            blue_num_buy = nums_pre(blue_num_buy)
        
            # 判断红球
            for red_result_item in red_nums_result:
                for red_buy_item in red_nums_buy:
                    if red_result_item == red_buy_item:
                        red_lucky_count += 1
        
            # 判断蓝球
            if blue_num_result == blue_num_buy:
                blue_lucky_count = 1
        
            # 据福彩双色球的中奖规则所写,包括了所有的红蓝组合以及相对应的中奖情况
            if red_lucky_count == 6 and blue_lucky_count == 1:
                luck_level = 1  # 一等奖(6+1)
            elif red_lucky_count == 6 and blue_lucky_count == 0:
                luck_level = 2  # 二等奖(6+0)
            elif red_lucky_count == 5 and blue_lucky_count == 1:
                luck_level = 3  # 三等奖(5+1)
            elif red_lucky_count == 5 and blue_lucky_count == 0:
                luck_level = 4  # 四等奖(5+0)
            elif red_lucky_count == 4 and blue_lucky_count == 1:
                luck_level = 4  # 四等奖(4+1)
            elif red_lucky_count == 4 and blue_lucky_count == 0:
                luck_level = 5  # 五等奖(4+0)
            elif red_lucky_count == 3 and blue_lucky_count == 1:
                luck_level = 5  # 五等奖(3+1)
            elif red_lucky_count == 0 and blue_lucky_count == 1:
                luck_level = 6  # 六等奖(0+1)
            elif red_lucky_count == 1 and blue_lucky_count == 1:
                luck_level = 6  # 六等奖(1+1)
            elif red_lucky_count == 2 and blue_lucky_count == 1:
                luck_level = 6  # 六等奖(2+1)
            else:
                luck_level = -1
        
            return __get_lucky_desc(luck_level),luck_level
        

        5. 总结一下

        通过上面的几个步骤,我们实现了福彩双色球的选号、爬取中奖号码、判断是否中奖等功能,彩票完全自动化还有部分内容,在后面文章我们再细聊

        相比体彩大乐透,福彩双色球虽然奖项配置少一点,但是在尾部奖项上更容易中取;彩票作为一项公益事业,建议大家抱着做公益、娱乐的性质理性购买

        声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。