【python爬虫及邮件】体彩大乐透定时查询、比对以及发送邮箱

【python爬虫及邮件】体彩大乐透定时查询、比对以及发送邮箱


前言

无意中了解到,家里人有买大乐透的习惯,而且有3个号追了很多年。每次都是追加投注很多期。但是由于工作忙,总是忘了查询、兑奖。(小奖也是奖啊~)于是乎,用刚学的Python写了这个脚本。

主要分为三个步骤,一是先在官网上爬取开奖结果,二是比对中奖结果,三是将中奖结果发送邮箱。
脚本设置每周一三六晚上10:30定时执行。

1.爬取开奖结果

// 爬取开奖结果
import requests
from bs4 import BeautifulSoup
def inquire_num():
    res=requests.get('https://webapi.sporttery.cn/gateway/lottery/getDigitalDrawInfoV1.qry?param=85,0&isVerify=1')
    data=dict['value']['dlt']['lotteryDrawResult']
    phase=dict['value']['dlt']['lotteryDrawNum']
    print('第%s期的开奖结果是:%s'%(phase,data))

2.比对中奖结果

// 比对中奖结果
def compare(blue_num_s,yellow_num_s):
    data_num=data.replace(" ","")//把爬取的开奖结果data中的空格去掉
    blue_num_c=[]
    yellow_num_c=[]
    //取出开奖蓝色球的数值
    for x in range(0,5):
        blue_num_c.append(int(data_num[2*x]+data_num[2*x+1]))
    //取出开奖黄色球的数值
    for x in range(5,7):
        yellow_num_c.append(int(data_num[2*x]+data_num[2*x+1]))
    i = j = n_blue = 0
    //分别比较买的号码和开奖号码中篮球、黄球数值相同的数量
    while i <= len(blue_num_s)-1 and j <= len(blue_num_s)-1:
        if blue_num_s[i] == blue_num_c[j]:
            n_blue+=1
        if blue_num_s[i] <= blue_num_c[j]:
            i += 1
        else:
            j += 1
    i = j = n_yellow = 0
    while i <= len(yellow_num_s)-1 and j <= len(yellow_num_s)-1:
        if yellow_num_s[i] == yellow_num_c[j]:
            n_yellow+=1
        if yellow_num_s[i] <= yellow_num_c[j]:
            i += 1
        else:
            j += 1
    //计算奖项大小
    if n_blue == 5 and n_yellow == 2:
        content = '第'+str(a+1)+'组号码'+'中了一等奖\n'
    elif n_blue == 5 and n_yellow == 1:
        content = '第'+str(a+1)+'组号码'+'中了二等奖\n'
    elif n_blue == 5:
        content = '第'+str(a+1)+'组号码'+'中了三等奖\n'
    elif n_blue == 4 and n_yellow == 2:
        content = '第'+str(a+1)+'组号码'+'中了四等奖\n'
    elif n_blue == 4 and n_yellow == 1:
        content = '第'+str(a+1)+'组号码'+'中了五等奖\n'
    elif n_blue == 3 and n_yellow == 2:
        content = '第'+str(a+1)+'组号码'+'中了六等奖\n'
    elif n_blue == 4:
        content = '第'+str(a+1)+'组号码'+'中了七等奖\n'
    elif (n_blue == 2 and n_yellow == 2) or (n_blue == 3 and n_yellow == 1):
        content = '第'+str(a)+'组号码'+'中了八等奖\n'
    elif (n_blue == 1 and n_yellow == 2) or (n_yellow == 2) or (n_blue == 2 and n_yellow == 1):
        content = '第'+str(a+1)+'组号码'+'中了九等奖\n'
    else:
        content = '第'+str(a+1)+'组号码'+'未中奖\n'

3.将中奖结果发送邮箱

// 将中奖结果发送邮箱
import smtplib
from email.mime.text import MIMEText
from email.header import Header
def mail_qq(text):
    mailhost='smtp.qq.com'
    qqmail = smtplib.SMTP()
    qqmail.connect(mailhost,25)
    account = '********@qq.com'//这里输入自己的邮箱
    password = '******'//这里输入自己邮箱授权码
    qqmail.login(account,password)
    receiver='*********qq.com'//这里输入接收者邮箱
    message = MIMEText(text, 'plain', 'utf-8')#
    subject = '彩票开奖结果'
    message['Subject'] = Header(subject, 'utf-8')
    try:
        qqmail.sendmail(account,receiver, message.as_string())
        print ('邮件发送成功')
    except:
        print ('邮件发送失败')
    qqmail.quit()

4.main()

def main():
//*填入自己买的号(家人长期坚持守了3个号)
    blue=[[1,2,3,4,5],[*,*,*,*,*],[*,*,*,*,*]]
    yellow=[[5,6],[*,*],[*,*]]
    text='第'+phase+'期大乐透的开奖结果是'+data+'\n'
    inquire_num()
    for a in range(0,3):
        compare(blue[a],yellow[a])
        text=text+content
    mail_qq(text)

5.设置定时任务

用crontab,设置每周一、三、六的定时任务
30 22 * * 1,3,6 /lottory.py

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值