实现思路:
1.通过urllib库爬取http://zx.500.com/dlt/页面,并过滤出信息
2.将自己的买的彩票的号与开奖号进行匹配,查询是否中奖
3.将中奖结果发生到自己邮箱
caipiao.py #获取最新一期彩票开奖结果
# -*- coding:utf- -*-
# @Time: -- :
import re
import urllib
import time
import sys
def get_winnum():
datapath = sys.path[]
datasuffix = 'txt'
if (len(sys.argv)>):
datapath = sys.argv[]
datasuffix = sys.argv[] def getHtml(url):
html = urllib.urlopen(url)
return html.read() html = getHtml("http://zx.500.com/dlt/") reg = ['<dt>([0-9]\d*).*</dt>']
reg.append('<li class="redball">([0-9]\d*)</li>')
reg.append('<li class="blueball">([0-9]\d*)</li>') outstr = "";
for i in range(len(reg)):
page = re.compile(reg[i])
rs = re.findall(page,html)
for j in range(len(rs)):
outstr+= rs[j] + "," #print time.strftime('%Y-%m-%d',time.localtime(time.time()))+":"+outstr[:-] with open(datapath+'/lot_500_dlt.'+datasuffix, 'a') as f:
f.write(time.strftime('%Y-%m-%d',time.localtime(time.time()))+":"+outstr[:-]+'\n') if __name__ == '__main__':
get_winnum()
sendmail.py #发送邮件
# -*- coding:utf- -*-
# Author: xueminchao
# @Time: -- : import smtplib
from email.mime.text import MIMEText def mail(sub,cont):
sender = '[email protected]' #发送人邮箱
passwd = "xxxx" #发送人邮箱授权码
receivers = '[email protected]' #收件人邮箱 subject = sub#主题
content = cont #正文 msg = MIMEText(content,'plain','utf-8')
msg['Subject'] = subject
msg['From'] = sender
msg['TO'] = receivers
try:
s = smtplib.SMTP_SSL('smtp.qq.com',)
s.login(sender,passwd)
s.sendmail(sender,receivers,msg.as_string())
print('发送成功')
except Exception:
print('发送失败')
if __name__ == '__main__':
mail(subject,content)
query_win.py #查询是否中奖
# -*- coding:utf- -*-
# Author:
# @Time: -- :
import os
import sys
import caipiao
import send_mail
def win_rules(num_list,last_results):
my_blue = num_list[:]
my_red = num_list[:]
result_blue = last_results[:]
result_red = last_results[:]
same_blue = [l for l in my_blue if l in result_blue]
same_red = [l for l in my_red if l in result_red ]
same_num_blue = len(same_blue)
same_num_red = len(same_red)
subject="大乐透中奖查询"
content="未中奖" #9等奖
if same_num_blue == and same_num_red == :
print("\033[1;31;0m你已经中了一等奖,中奖号码为 %s \033[0m" % num_list)
content = "你已经中了一等奖,中奖号码为 " + str(num_list)
elif same_num_blue == and same_num_red == :
print("\033[1;35;0m你已经中了二等奖,中奖号码为 %s \033[0m" % num_list) content = "你已经中了二等奖,中奖号码为 " + str(num_list)
elif same_num_blue == :
print("\033[1;33;0m你已经中了三等奖, 中奖号码为 %s \033[0m" % num_list)
content = "你已经中了三等奖,中奖号码为 " + str(num_list)
elif same_num_blue == and same_num_red == :
print("\033[1;32;0m你已经中了四等奖, 中奖号码为 %s \033[0m" % num_list)
content = "你已经中了四等奖,中奖号码为 " + str(num_list)
elif same_num_blue == and same_num_red == :
print("\033[1;32;0m你已经中了五等奖, 中奖号码为 %s \033[0m" % num_list)
content = "你已经中了一等奖,中奖号码为 " + str(num_list)
elif same_num_blue == and same_num_red == :
print("\033[1;34;0m你已经中了六等奖, 中奖号码为 %s\033[0m" % num_list)
content = "你已经中了六等奖,中奖号码为 " + str(num_list)
elif same_num_blue == :
print("\033[1;34;0m你已经中了七等奖, 中奖号码为 %s \033[0m" % num_list)
content = "你已经中了七等奖,中奖号码为 " + str(num_list)
elif (same_num_blue == and same_num_red == ) or (same_num_blue == and same_num_red == ):
print("\033[1;34;0m你已经中了八等奖, 中奖号码为 %s \033[0m " % num_list)
content = "你已经中了八等奖,中奖号码为 " + str(num_list)
elif (same_num_blue == and same_num_red == ) or (same_num_red == ) or (same_num_blue == and same_num_red == ):
print("\033[1;36;0m你已经中了九等奖, 中奖号码为 %s \033[0m " % num_list)
content = "你已经中了九等奖,中奖号码为 " + str(num_list)
else:
print("sorry,你没有中奖!!! ")
content = "未中奖"+ "本次开奖号码为"+ str(last_results)
send_mail.mail(subject, content) if __name__ == '__main__':
caipiao.get_winnum()
datepath = datapath = sys.path[]
fname="lot_500_dlt.txt"
fmy_num="my_num.txt"
with open(datapath+'/'+ fname,'r') as f:
lines = f.readlines() #读取所有行
last_line = lines[-].strip('\n')
#获取最新的彩票号码
last_results=last_line.split(",")[:]
#查询是否中奖,获取自己的号码
with open(datapath+'/'+ fmy_num,'r') as f:
lines= f.readlines()
for i in lines:
i=i.strip('\n')
num=i.split(',')
win_rules(num,last_results)
设置为定时任务
* * /usr/bin/python /home/xmc/appl/caipiao/query_win.py > /dev/null >& &
说明:
my_num.txt 是自己买的彩票号码存放位置,注意数字必须为两位数,并且以逗号分隔,每一组为一行
,,,,,,
,,,,,,
,,,,,,
参考
https://www.jianshu.com/p/a3ddf9333b3f
https://www.cnblogs.com/lizhe860/p/9079234.html