• Python学习笔记四


    #模拟发送邮件

     1 import smtplib
     2 from email.mime.text import MIMEText
     3 
     4 msg_from="****@163.com"#发送方
     5 pwd="****"#密码
     6 to="****@qq.com"#接收方
     7 
     8 subject="这是Python发送的邮件!"
     9 content="你监控的数据的值已经达到,请注意!"
    10 
    11 #构造邮件
    12 msg=MIMEText(content) #邮件对象
    13 msg["Subject"]=subject
    14 msg["From"]=msg_from
    15 msg["To"]=to
    16 
    17 #发送邮件
    18 try:
    19     ss=smtplib.SMTP_SSL("smtp.163.com",465)
    20     ss.login(msg_from,pwd)
    21     ss.sendmail(msg_from,to,msg.as_string())#发送
    22     print("发送成功!")
    23 except Exception as e:
    24     print("发送失败!详情:",e)
    View Code

    #股票提醒系统

     1 #股票提醒系统 tushare
     2 
     3 import tushare
     4 import time
     5 import smtplib
     6 from email.mime.text import MIMEText
     7 
     8 #获取股票数据
     9 def getrealtimedate(share):
    10     dataNow=tushare.get_realtime_quotes(share.code)
    11     share.name=dataNow.loc[0][0]
    12     share.price=float(dataNow.loc[0][3])
    13     share.high=dataNow.loc[0][4]
    14     share.low=dataNow.loc[0][5]
    15     share.volum=dataNow.loc[0][8]
    16     share.amount=dataNow.loc[0][9]
    17     share.openToday=dataNow.loc[0][1]
    18     share.pre_close=dataNow.loc[0][2]
    19     share.time1=dataNow.loc[0][30]
    20     share.descrbe="股票名:"+share.name+"当前价格:"+str(share.price)
    21     
    22     return share
    23 
    24 #发送邮件
    25 def sendmail(subject,content):
    26     msg_from="****@163.com"#发送方
    27     pwd="****"#密码
    28     to="****@qq.com"#接收方
    29 
    30     #构造邮件
    31     msg=MIMEText(content) #邮件对象
    32     msg["Subject"]=subject
    33     msg["From"]=msg_from
    34     msg["To"]=to
    35 
    36     #发送邮件
    37     try:
    38         ss=smtplib.SMTP_SSL("smtp.163.com",465)
    39         ss.login(msg_from,pwd)
    40         ss.sendmail(msg_from,to,msg.as_string())#发送
    41     except Exception as e:
    42         print("发送失败!详情:",e)
    43 
    44 #股票类 
    45 class Share():
    46     def __init__(self,code,buy,sale):
    47         self.name=""
    48         self.price=""
    49         self.high=""
    50         self.low=""
    51         self.volum=""
    52         self.amount=""
    53         self.openToday=""
    54         self.pre_close=""
    55         self.time1=""
    56         self.descrbe=""
    57         self.code=code
    58         self.buy=buy
    59         self.sale=sale
    60 
    61 
    62 def main(sharelist):
    63     
    64     for share in sharelist:
    65         
    66         sss=getrealtimedate(share)
    67         print (sss.descrbe)
    68 
    69         if sss.price<=sss.buy:
    70             print("达到买点,如果有钱请赶紧买!")
    71             sendmail("达到买点,",sss.descrbe)
    72         elif sss.price>=sss.sale:
    73             print("达到卖点,手里有货赶紧卖!")
    74             sendmail("达到卖点,",sss.descrbe)
    75         else:
    76             print("不要买卖,等着!")
    77 
    78 
    79 while 1==1:
    80     
    81     share1=Share("000591",3.3,3.6)
    82     share2=Share("601988",3.3,3.6)
    83     share3=Share("000034",3.3,3.6)
    84 
    85     list1=[share1,share2,share3]
    86     print("================")
    87 
    88     main(list1)
    89     time.sleep(600)#每隔10分钟监控一次
    View Code

    如果没有tushare包,记得安装该包,pip install tushare

  • 相关阅读:
    汉诺塔
    美丽联合2018前端
    游览器
    python多线程实现
    cuda toolkit
    编译器,解释器及混合编译
    大疆无人机
    SaaS PaaS IaaS mes
    CNN中权值共享的理解
    最近动作项目心得
  • 原文地址:https://www.cnblogs.com/zwh-Seeking/p/11672201.html
Copyright © 2020-2023  润新知