脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|shell|

服务器之家 - 脚本之家 - Python - Python实现双色球号码随机生成

Python实现双色球号码随机生成

2023-02-10 11:00煎蛋哥 Python

和体彩大乐透类似,福彩双色球也是购买次数最多的彩种之一,相比大乐透,双色球更容易中小奖。本文将介绍 Python 实习双色球彩票自由的流程,感兴趣的可以了解一下

大家好,我是煎蛋哥!

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

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

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

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

1. 随机一注

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

其中

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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(133)
            if temp_red_num not in reds:
                reds.append(temp_red_num)
 
        # 蓝球
        blue = random.randint(116)
 
        # 红球排序
        reds.sort()
 
        # 数据预处理
        reds = nums_pre(reds)
        blue = nums_pre([blue])[0]
 
        result.append(' '.join(reds) + " + " + blue)
    return '\n'.join(result)

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
def nums_pre(nums):
    """
    购买数字预处理,如果是个位数,加上0
    :param nums:
    :return:
    """
    if nums:
        if isinstance(nums, listor 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. 红球固定或蓝球固定

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

红球固定

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
def gene_blue_random_ssq(reds, number):
    """
    红球固定,蓝球随机
    :param reds:
    :param number:
    :return:
    """
    result = []
 
    for item in range(number):
        # 蓝球
        blue = random.randint(116)
 
        # 红球排序
        reds.sort()
 
        # 数据预处理
        reds = nums_pre(reds)
        blue = nums_pre([blue])[0]
 
        result.append(' '.join(reds) + " + " + blue)
    return '\n'.join(result)

蓝球固定

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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(133)
            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 数据展示了过去每一期双色球的中奖号码,我们只需要使用正则表达式匹配出所有中奖号码,取最近的一期号码即可

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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. 是否中奖

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

实现代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
...
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. 总结一下

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

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

到此这篇关于Python实现双色球号码随机生成的文章就介绍到这了,更多相关Python双色球号码生成内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://mp.weixin.qq.com/s/VRySlFjobScBCsoWIzt1uA

延伸 · 阅读

精彩推荐