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("本组号码未曾中过头奖,祝您中奖")