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 =  ['<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[:-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 = 'xxxx@qq.com'   #發送人郵箱
    passwd = "xxxx" #發送人郵箱授權碼
    receivers = 'xxxxxx@qq.com' #收件人郵箱

    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

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM