python 获取大乐透中奖结果

实现思路:

  1.通过urllib库爬取http://zx.500.com/dlt/页面,并过滤出信息

  2.将自己的买的彩票的号与开奖号进行匹配,查询是否中奖

  3.将中奖结果发生到自己邮箱

 

caipiao.py  #获取最新一期彩票开奖结果

# -*- coding:utf-8 -*-
# @Time: 2019-08-05 9:48
import re
import urllib
import time
import sys
def get_winnum():
    datapath = sys.path[0]
    datasuffix = 'txt'
    if (len(sys.argv)>1):
        datapath = sys.argv[1]
        datasuffix = sys.argv[2]

    def getHtml(url):
        html = urllib.urlopen(url)
        return html.read()


    html = getHtml("http://zx.500.com/dlt/")


    reg =  ['
([0-9]\d*).*
'] reg.append('
  • ([0-9]\d*)
  • ') reg.append('
  • ([0-9]\d*)
  • ') 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[:-1] with open(datapath+'/lot_500_dlt.'+datasuffix, 'a') as f: f.write(time.strftime('%Y-%m-%d',time.localtime(time.time()))+":"+outstr[:-1]+'\n') if __name__ == '__main__': get_winnum()

    sendmail.py  #发送邮件

    # -*- coding:utf-8 -*-
    # Author: xueminchao
    # @Time: 2019-08-05 15:00
    
    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',465)
            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-8 -*-
    # Author: 
    # @Time: 2019-08-05 10:08
    import os
    import sys
    import caipiao
    import send_mail
    def win_rules(num_list,last_results):
        my_blue =  num_list[0:5]
        my_red = num_list[5:]
        result_blue = last_results[0:5]
        result_red = last_results[5:]
        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 == 5 and same_num_red == 2:
            print("\033[1;31;0m你已经中了一等奖,中奖号码为 %s \033[0m" % num_list)
            content = "你已经中了一等奖,中奖号码为 " + str(num_list)
        elif same_num_blue == 5 and same_num_red == 1:
            print("\033[1;35;0m你已经中了二等奖,中奖号码为 %s \033[0m" % num_list)
    
            content = "你已经中了二等奖,中奖号码为 " + str(num_list)
        elif same_num_blue == 5:
            print("\033[1;33;0m你已经中了三等奖, 中奖号码为 %s \033[0m" % num_list)
            content = "你已经中了三等奖,中奖号码为 " + str(num_list)
        elif same_num_blue == 4 and same_num_red == 2:
            print("\033[1;32;0m你已经中了四等奖, 中奖号码为 %s \033[0m" % num_list)
            content = "你已经中了四等奖,中奖号码为 " + str(num_list)
        elif same_num_blue == 4 and same_num_red == 1:
            print("\033[1;32;0m你已经中了五等奖, 中奖号码为 %s \033[0m" % num_list)
            content = "你已经中了一等奖,中奖号码为 " + str(num_list)
        elif same_num_blue == 3 and same_num_red == 2:
             print("\033[1;34;0m你已经中了六等奖, 中奖号码为 %s\033[0m" % num_list)
             content = "你已经中了六等奖,中奖号码为 " + str(num_list)
        elif same_num_blue == 4:
            print("\033[1;34;0m你已经中了七等奖, 中奖号码为 %s \033[0m" % num_list)
            content = "你已经中了七等奖,中奖号码为 " + str(num_list)
        elif (same_num_blue == 2 and same_num_red == 2) or (same_num_blue == 3 and same_num_red == 1):
            print("\033[1;34;0m你已经中了八等奖, 中奖号码为 %s \033[0m " % num_list)
            content = "你已经中了八等奖,中奖号码为 " + str(num_list)
        elif (same_num_blue == 1 and same_num_red == 2) or (same_num_red == 2) or (same_num_blue == 2 and same_num_red == 1):
            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[0]
        fname="lot_500_dlt.txt"
        fmy_num="my_num.txt"
        with open(datapath+'/'+ fname,'r') as f:
            lines = f.readlines()   #读取所有行
            last_line = lines[-1].strip('\n')
        #获取最新的彩票号码
        last_results=last_line.split(",")[1:]
        #查询是否中奖,获取自己的号码
        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)

     

    设置为定时任务

    30 21 * * 6 /usr/bin/python  /home/xmc/appl/caipiao/query_win.py  >  /dev/null 2>&1 &

    说明:

      my_num.txt 是自己买的彩票号码存放位置,注意数字必须为两位数,并且以逗号分隔,每一组为一行

    11,16,20,27,34,03,09
    06,14,18,22,23,10,12
    09,14,18,33,34,03,12

     参考

    https://www.jianshu.com/p/a3ddf9333b3f

    https://www.cnblogs.com/lizhe860/p/9079234.html

      

     

    转载于:https://www.cnblogs.com/xmc2017/p/11303586.html

    你可能感兴趣的:(python 获取大乐透中奖结果)