一个简单的双色球选号python程序

这段代码使用Python随机生成一组彩票号码,并检查该组号码是否曾出现在历史彩票数据中。通过请求网页获取历史彩票数据,利用lxml库解析HTML并进行比较。如果号码匹配,则提示已中过头奖,否则输出机选号码并祝好运。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import random
import requests
import operator
from lxml import etree

def generate_lucky_numbers_with_exclusion(exclude_reds, exclude_blue):
    # 从1到33中随机选取6个不重复的数,且不在红球排除数组中
    possible_reds = [i for i in range(1, 34) if i not in exclude_reds]
    red_balls = random.sample(possible_reds, 6)
    red_balls.sort()  # 排序

    # 从1到16中随机选取一个数,且不在蓝球排除数组中
    possible_blues = [i for i in range(1, 17) if i not in exclude_blue]
    blue_ball = random.choice(possible_blues)

    return red_balls, blue_ball

# 用户输入的红球和蓝球排除数组
user_exclude_reds = [2.15]  # 红球排除数组
user_exclude_blue = [3]  # 蓝球排除数组

reds, blue = generate_lucky_numbers_with_exclusion(user_exclude_reds, user_exclude_blue)

#与彩票网站数据进行比较
url = "http://datachart.500.com/ssq/history/newinc/history.php?start=00001&end=24141"
response = requests.get(url)
response = response.text
selector = etree.HTML(response)
for i in selector.xpath('//tr[@class="t_tr1"]'):
    datetime = i.xpath('td/text()')[0]
    old_red = list(map(int,i.xpath('td/text()')[1:7]))
    old_blue = int(i.xpath('td/text()')[7])
    if(operator.eq(reds,old_red) and operator.eq(blue,old_blue)):
        print("您机选的这组号码已经中过头奖了,建议重选")

#输出最终的号码
print("机选号码:",reds," ",blue,)
print("本组号码未曾中过头奖,祝您中奖")

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值