名称:

双色球中奖邮件通知

功能:

1、每个开奖日后第二天以邮件方式通知是否中奖。

思路:

1、使用Python,跨平台,三方库功能强大

2、从网页上爬取当前的开奖号码 http://cp.360.cn/kj/ssq.html?agent=700007

3、与自己填写的号码检测是否匹配中奖

4、判断后发送邮件通知

5、每周2、4、7开奖,则每周3、5、1凌晨1点(可修改为上午十点最好)判断并发送。

-- coding: UTF-8 --

import smtplib
import urllib2
import string
import datetime
import time

from email.mime.text import MIMEText

from email.MIMEText import MIMEText
from email.Header import Header

我的号码

my_code_six = [1, 4, 8, 15, 17, 28]
my_code_one = 3

网页地址

web_url = "http://cp.360.cn/kj/ssq.html?agent=700007"

彩票开奖内容

lottery_date = ""
lottery_code_six = []
lottery_code_one = 0

号码匹配

winning_red_count = 0;
winning_blue_count = 0;

mailto_list=";"
mail_host="" #设置服务器
mail_user="" #用户名
mail_pass="" #口令
mail_postfix="163.com" #发件箱的后缀

def send_mail(to_list,sub,content):

global mailto_list
global mail_host
global mail_user
global mail_pass
global mail_postfix
     
me="中大奖"+"<"+mail_user+"@"+mail_postfix+">" 
msg = MIMEText(content,_subtype='plain',_charset='gb2312')  
msg['Subject'] = sub  
msg['From'] = me  
msg['To'] = to_list
try:  
    server = smtplib.SMTP()  
    server.connect(mail_host)  
    server.login(mail_user,mail_pass)  
    server.sendmail(me, to_list, msg.as_string())  
    server.close()  
    return True 
except Exception, e:  
    print str(e)  
    return False 

def check_lottery():

global lottery_date
global winning_red_count
global winning_blue_count
global lottery_code_six
global lottery_code_one
global my_code_one
global my_code_six
global web_url
 
winning_red_count = 0
winning_blue_count = 0
 
response = urllib2.urlopen(web_url)
html = response.read()
#print html
 
#date
key = "option value='"
nPos = html.index(key)
if nPos <= 0:
    return 0
html = html[nPos + len(key) : nPos + len(key) + 37]
print 'GET NEW RESULT:   ' + html
nPos = html.index("'")
if nPos <= 0:
    return 0
     
lottery_date = html[0 : nPos]
#print lottery_date
 
#first code
key = "code='"
nPos = html.index(key)
if nPos <= 0:
    return 0
html = html[nPos + len(key) : ]
nPos = html.index(" ")
if nPos <= 0:
    return 0
first_code = html[0 : nPos]
#print first_code
html = html[3 : ]
#print html
 
#second code
key = " "
nPos = html.index(key)
if nPos <= 0:
    return 0
 
second_code = html[0 : nPos]
#print second_code
html = html[3 : ]
#print html
 
#three code
key = " "
nPos = html.index(key)
if nPos <= 0:
    return 0
 
three_code = html[0 : nPos]
#print three_code
html = html[3 : ]
 
#four code
key = " "
nPos = html.index(key)
if nPos <= 0:
    return 0
 
four_code = html[0 : nPos]
#print four_code
html = html[3 : ]
 
#five code
key = " "
nPos = html.index(key)
if nPos <= 0:
    return 0
 
five_code = html[0 : nPos]
#print five_code
html = html[3 : ]
 
#six code
key = "+"
nPos = html.index(key)
if nPos <= 0:
    return 0
 
six_code = html[0 : nPos]
#print six_code
html = html[3 : ]
 
#one code
key = "'"
nPos = html.index(key)
if nPos <= 0:
    return 0
 
one_code = html[0 : nPos]
 
lottery_code_six.append(first_code)
lottery_code_six.append(second_code)
lottery_code_six.append(three_code)
lottery_code_six.append(four_code)
lottery_code_six.append(five_code)
lottery_code_six.append(six_code)
 
lottery_code_one = string.atoi(one_code)
 
for lottry_code in lottery_code_six:
    for my_code in my_code_six:
        if string.atoi(lottry_code) == my_code:
            winning_red_count = winning_red_count + 1
            break
if my_code_one == lottery_code_one:
    winning_blue_count = 1
    print "ok"
 
print my_code_one
print one_code
 
need_send_email = 0;
#blue more than 1
if winning_blue_count >= 1 :
    need_send_email = 1
     
#red more than 4
if winning_red_count >= 4:
    need_send_email = 1
     
print need_send_email
return need_send_email

def check_date():

date_vaule = datetime.datetime.now().weekday() + 1
print 'date_vaule:' + str(date_vaule)
if ((date_vaule != 1) and (date_vaule != 3) and (date_vaule != 5)):
    return 0
     
hour = time.strftime("{b75a474a571334bb08f4db31fa80d7688c6401b1dcf97fb55e06ed241b59472c}H", time.localtime()) 
minute = time.strftime("{b75a474a571334bb08f4db31fa80d7688c6401b1dcf97fb55e06ed241b59472c}M", time.localtime()) 
print 'hour:' + hour + '. minute:' + minute
if string.atoi(hour) != 1 or string.atoi(minute) != 0:
    return 0
 
print 'date_vaule:' + str(date_vaule) + '.hour:' + hour + '.minute:' + minute
return 1
 

if name == '__main__':


while True:
    if check_date() <= 0:
        print 'do noting'
        time.sleep(40)
         
    else:
        print "It's time to check lottery!!"
        result = check_lottery()
        print result
         
        send_message = "您的号码:"
        for my in my_code_six:
            send_message += str(my) + ","
        send_message += ":" + str(my_code_one) + "    "
         
        send_message += "开奖号码:"
         
        for lottery in lottery_code_six:
            send_message += str(lottery) + ','
        send_message += ":" + str(lottery_code_one)
         
        print send_message

        head = ""

        if  result > 0:
            head = "恭喜您中奖了,去看看吧"
             
        else :
            head = "下次一定能中的"
        print head

        if send_mail(mailto_list,head, send_message):  
                print "Send email ok" 
        else:  
            print "Send email faile"
        time.sleep(70)

标签: none

添加新评论