python 获取大乐透中奖结果

news/2024/7/19 10:11:39 标签: 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

  

 

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


http://www.niftyadmin.cn/n/1052733.html

相关文章

Unable to find a version of the runtime to run this application.

换装系统后&#xff0c;jsonView无法打开&#xff0c;出现的错误如下&#xff1a; Unable to find a version of the runtime to run this application. 解决方法&#xff1a;http://www.microsoft.com/zh-cn/download/details.aspx?id22下载后安装 Microsoft Microsoft .NET …

jar包中的jar包或者class文件替换

可以在windows下用winrar打开jar包&#xff0c;然后将需要修改的jar包或者class文件拖至winrar解压界面即可。该方法在linux下有可能不可用。转载于:https://www.cnblogs.com/yanhaidong/archive/2012/02/09/2344432.html

IE6死后快人心的十件事

很多人以为IE6已经死了&#xff0c;也许只有设计师这样认为&#xff0c;现实世界中IE6的使用者大有人在&#xff0c;不过不会维持很久&#xff0c;IE8已经推出&#xff0c;微软对IE8的推广不遗余力&#xff0c;同时&#xff0c;不少人从IE转到别的浏览器&#xff0c;总有一天&a…

2012年8月30日

今天丢失了Maven实战&#xff0c;和一本java并发实战&#xff0c;被当做垃圾给扔掉啦&#xff0c;哭呀

CentOS Linux 升级内核步骤、方法

当前系统为CentOS Linux release 6.0 (Final)&#xff0c;内核版本为2.6.32-71.el6.i686.由于最近内核出现最新的漏洞(linux kernel 又爆内存提权漏洞&#xff0c;2.6.39 内核无一幸免&#xff0c;所以将内核升级至3.2.2最新版本。1. 查看当前系统内核# uname -r 2 2.6.32-71.e…

SQLServer函数 left()、charindex()、stuff()的使用

1、left() LEFT (<character_expression>&#xff0c; <integer_expression>) 返回character_expression 左起 integer_expression 个字符。 结果&#xff1a; 2.charindex() CHARINDEX (<’substring_expression’>&#xff0c; <expression>) 返…

心甘情愿

2012年8月31日&#xff0c;夜深了&#xff0c;有点睡不着&#xff0c;看看书听听情歌&#xff0c;想想思念的人。

BZOJ.1021.[SHOI2008]循环的债务(DP)

题目链接 不同面额的钞票是可以分开考虑的。 ↑其实并不很明白具体(证明?)&#xff0c;反正是可以像背包一样去做。 f[x][i][j]表示用前x种面额钞票满足 A有i元 B有j元 (C有sum-i-j)所需交换的最少数量((abs(ΔA)abs(ΔB)abs(ΔAΔB))/2)。 (i,j是在本来就有的钞票的基础上的&…