# Simulation send a message
. 1 Import smtplib 2 from email.mime.text Import MimeText . 3 . 4 msg_from = " ****@163.com " # sender . 5 pwd = " **** " # password . 6 to = " **** @ QQ .com " # recipient 7 8 Subject = " this is the message sent Python! " 9 Content = " your monitor has reached the value of data, please note! " 10 11 # construction Mail 12 msg = MimeText (Content) #Message object 13 is MSG [ " the Subject " ] = Subject 14 MSG [ " the From " ] = msg_from 15 MSG [ " the To " ] = to 16 . 17 # E-mail 18 is the 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 ()) # send 22 Print ( " sent successfully! ") 23 the except Exception AS E: 24- Print ( " Failed to send details:! " , E)
# Stock reminder system
1 # stock alert system tushare 2 . 3 Import tushare . 4 Import Time . 5 Import smtplib . 6 from email.mime.text Import MimeText . 7 . 8 # obtain stock data . 9 DEF getrealtimedate (Share): 10 dataNow = tushare.get_realtime_quotes (share.code) . 11 = share.name dataNow.loc [0] [0] 12 is share.price = a float (dataNow.loc [0] [. 3 ]) 13 is 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 " # sender 27 pwd = " **** " # password 28 to = " ****@qq.com " # recipient 29 30 # configured message 31 is MSG = MimeText ( Content) # message object 32 MSG [ " the subject " ] = subject 33 is MSG [ " the From " ] = msg_from 34 is MSG [ " the to " ] = to 35 36 #E-mail 37 [ the try : 38 is SS = smtplib.SMTP_SSL ( " smtp.163.com " , 465 ) 39 ss.login (msg_from, pwd) 40 ss.sendmail (msg_from, to, msg.as_string ()) # Send 41 is the except AS E Exception: 42 is Print ( " transmission failed before:! " , E) 43 is 44 is # stock class 45 class Share (): 46 is DEF the __init__ (Self, code, Buy, Sale): 47 the 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 report this content share in sharelist: 65 66 sss = getrealtimedate (report this content share) 67 Print (sss.descrbe) 68 69 IF sss.price <= sss.buy: 70 Print ( " achieve buy, if the money to buy please hurry! " ) 71 the sendmail ( " reaches buy, " , sss.descrbe) 72 elif sss.price> = sss.sale: 73 is Print ( " ! reaches selling their hand quickly sell goods " ) 74 the sendmail ( " reaches a selling point,",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 the time.sleep (600) # every 10 minutes to monitor a
If you do not tushare package, remember to install the package, pip install tushare