Python學習筆記四

#模擬發送郵件ide

 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

#股票提醒系統spa

 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 tusharecode

相關文章
相關標籤/搜索