同花顺 sendmessage python_python代码监控股票信息发邮件发短信

#!/usr/bin/python

# -*- coding: utf-8 -*-

#Filename:monitor_gp.py

#Author:云开sky

#win10下 添加到计划任务

#SCHTASKS /Create /TN monitor /SC MINUTE /MO 30 /ST 09:30 /ET 16:30 /ED 2020/06/19 /TR "C:\Users\songyunkui\AppData\Local\Microsoft\WindowsApps\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\python D:\work\python\monitor_gp.py"

import datetime

import os

import smtplib

from email.mime.text import MIMEText

from email.mime.multipart import MIMEMultipart

from email import encoders

from email.header import Header

from email.mime.base import MIMEBase

from urllib import request,error

import ssl

from pandas_datareader import data #查询股票信息 需要先安装下这个包 pip install pandas_datareader

#pip install tencentcloud-sdk-python #通过腾讯云发送短信息 需要先安装下这个包 pip install tencentcloud-sdk-python

from tencentcloud.common import credential

from tencentcloud.common.profile.client_profile import ClientProfile

from tencentcloud.common.profile.http_profile import HttpProfile

from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException

from tencentcloud.sms.v20190711 import sms_client, models

########################## 需更改的值

stock_code = "1810.hk"

#stock_code = input("美股直接输入股票代码如GOOG \n港股输入代码+对应股市,如腾讯:0700.hk \n国内股票需要区分上证和深证,股票代码后面加.ss或者.sz\n请输入你要查询的股票代码:")

stock_price = 16

#监控阈值

################## 发送邮件的

##邮箱发送服务器

mail_server = 'smtp.qq.com'

##邮箱发送服务器端口

mail_port = '587'

##发送邮件

mail_from = 'songyunkui@q.com'

##发送邮件密码

mail_pass = 'xxxxxx'

##接收邮件

mail_to = 'songyunkui@q.com'

##抄送邮件

mail_cc = 'songyunkui@x.com'

##########################

curr_time = datetime.datetime.now()

start_date = curr_time.strftime("%Y-%m-%d")

end_date = start_date

stock_info = data.get_data_yahoo(stock_code, start_date, end_date)

curr_price=stock_info.loc[start_date,'Close'] #当前收盘价

curr_price=round(curr_price,2)

##邮件标题

mail_subject = '股票'+stock_code+'达到阈值'

##邮件内容

mail_body = """

老板您好:

     您定义的股票:""" + stock_code+""" """ +curr_time.strftime('%Y-%m-%d%H:%M:%S')+ """ 已达到阈值:"""+ str(stock_price) +""" 现值:"""+str(round(curr_price,2))+ """ 请关注。

-------------------------------------------------------------------------------------

联系邮箱:songyunkui@q.com

"""

def send_mail():

# 构造一个MIMEMultipart对象代表邮件本身

msg = MIMEMultipart()

msg['From'] = mail_from

msg['To'] = mail_to

msg['Cc'] = mail_cc

mail_sub = mail_subject

msg['Subject'] = Header(mail_sub,'utf-8').encode()

msg.attach(MIMEText(mail_body,'html','utf-8'))

print(msg['To'])

try:

print('-----------------------------------------发送邮件开始')

print('--Program begin',datetime.datetime.now())

s = smtplib.SMTP()

s.connect(mail_server,mail_port)

s.login(mail_from,mail_pass)

##sendmail的第2个参数邮件地址是个list,所以将字符串用split转为list

s.sendmail(mail_from,mail_to.split(',')+mail_cc.split(','),msg.as_string())

s.quit()

print('-----------------------------------------发送邮件结束')

print('--Program begin',datetime.datetime.now())

except smtplib.SMTPException as e:

print('发送失败:',e)

return

##################发送短信的 基于腾讯云的短信服务 https://console.cloud.tencent.com/smsv2

def send_msg():

SecretId='AKID' #####腾讯云 API 密钥

SecretKey='R0HO'

tem_id='662xxx' ###短信服务模板ID

smssdk_id='1400xxxx' ###应用管理应用ID 这些都可以在腾讯云短信服务里申请

user='syk'

phone_nm='182xxxxxxxx'

try:

cred = credential.Credential(SecretId, SecretKey)

httpProfile = HttpProfile()

httpProfile.endpoint = "sms.tencentcloudapi.com"

clientProfile = ClientProfile()

clientProfile.httpProfile = httpProfile

client = sms_client.SmsClient(cred, "ap-beijing", clientProfile)

req = models.SendSmsRequest()

params = '{\"PhoneNumberSet\":[\"+86'+ phone_nm +'\"],\"TemplateID\":\"'+tem_id+'\",\"Sign\":\"abi数据化智能化\",\"TemplateParamSet\":[\"'+user+'\",\"'+stock_code.split('.')[0]+'\",\"'+str(stock_price)+'\",\"'+str(curr_price)+'\"],\"SmsSdkAppid\":\"'+smssdk_id+'\"}'

req.from_json_string(params)

resp = client.SendSms(req)

print(resp.to_json_string())

except TencentCloudSDKException as err:

print(err)

return

if curr_price >= stock_price:

send_mail()

send_msg()

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值