股市打新债卷,只要中签基本是稳赚不赔,但是也不是每天都有,天天看也麻烦。于是便有了下面的自动化程序。 import requestsimport timeimport smtplibfrom datetime import datetimefrom email.mime.text
股市打新债卷,只要中签基本是稳赚不赔,但是也不是每天都有,天天看也麻烦。于是便有了下面的自动化程序。
import requests
import time
import smtplib
from datetime import datetime
from email.mime.text import MIMEText
def web():
url = 'https://www.jisilu.cn/data/cbnew/pre_list/?___jsl=LST___t=1633347537744'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.61 Safari/537.36'
}
data = {
'progress': '',
'rp': '22',
'page': '1'
}
try:
dic_bond = requests.post(url, data, headers).json()
data_list = []
data_list2 = []
num = 0
num1 = 0
num2 = 0
num3 = 0
now_time = '今天可申购:'
future_time = '未来可申购:'
for i in range(len(dic_bond['rows'])):
if '' == dic_bond['rows'][i]['cell']['bond_id']:
break
bond_id = dic_bond['rows'][i]['cell']['bond_id']
bond_nm = dic_bond['rows'][i]['cell']['bond_nm']
progress_nm = dic_bond['rows'][i]['cell']['progress_nm']
rating_cd = dic_bond['rows'][i]['cell']['rating_cd']
stock_nm = dic_bond['rows'][i]['cell']['stock_nm']
amount = dic_bond['rows'][i]['cell']['amount']
d1 = datetime.strptime(time.strftime("%Y-%m-%d", time.localtime()), '%Y-%m-%d')
d2 = datetime.strptime(progress_nm[:10], '%Y-%m-%d')
if d1 > d2:
break
elif d1 == d2:
if '申购'in progress_nm:
num += 1
# now_time = '<h3>今天可申购:</h3>'
content = '<br \>代码:' + bond_id + '<br \>名称:' + bond_nm + '<br \>正股名称:' + stock_nm + '<br \><font color="red">时间:' + progress_nm[:12] + '</font>'+ '<br \>' + '评级: ' + rating_cd + '<br \>'+ '发行规模(亿元):' + amount + '<br \> <br \>'
# data_list1.append(now_time)
data_list.append(content)
if '上市'in progress_nm:
num1 += 1
# now_time = '<h3>今天可申购:</h3>'
# content1 = '<br \>代码:' + bond_id + '<br \>名称:' + bond_nm + '<br \>正股名称:' + stock_nm + '<br \>时间:' + progress_nm[:12] + '<br \>' + '评级: ' + rating_cd + '<br \>'+ '发行规模(亿元):' + amount + '<br \> <br \>'
content1 = '<br \>代码:' + bond_id + '<br \>名称:' + bond_nm + '<br \>正股名称:' + stock_nm + '<br \>时间:' + progress_nm[:12] + '<br \>' + '评级: ' + '发行规模(亿元):' + amount + '<br \> <br \>'
# data_list1.append(now_time)
data_list.append(content1)
elif d1 < d2:
if '申购'in progress_nm:
num2 += 1
# future_time = '<h3>未来可申购:</h3>'
content = '<br \>代码:' + bond_id + '<br \>名称:' + bond_nm + '<br \>正股名称:' + stock_nm + '<br \><font color="green">时间:' + progress_nm[:12] + '</font>' + '<br \>' + '评级: ' + rating_cd + '<br \>'+ '发行规模(亿元):' + amount + '<br \> <br \>'
# data_list2.append(future_time)
data_list2.append(content)
if '上市'in progress_nm:
num3 += 1
# now_time = '<h3>今天可申购:</h3>'
content1 = '<br \>代码:' + bond_id + '<br \>名称:' + bond_nm + '<br \>正股名称:' + stock_nm + '<br \>时间:' + progress_nm[:12] + '<br \>' + '评级: ' + rating_cd + '<br \>'+ '发行规模(亿元):' + amount + '<br \> <br \>'
# data_list1.append(now_time)
data_list2.append(content1)
now_time += str(num)
now_time = "<h3><font color='red'>" + now_time + "</font></h3>"
future_time += str(num2)
future_time = "<hr /><h3><font color='green'>" + future_time + "</font></h3>"
data_list.insert(0,now_time)
data_list2.insert(0,future_time)
data_list.extend(data_list2)
# else:
# content = '代码:' + bond_id + '<br \>名称:' + bond_nm + '<br \>正股名称:' + stock_nm + '<br \>时间:' + progress_nm[:12] + '<br \>' + '评级: ' + rating_cd + '<br \>'+ '发行规模(亿元):' + amount + '<br \> <br \>'
# data_list.append(content)
print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
str_data = ''.join(data_list)
print(str_data)
email(str_data)
except IOError:
print('异常!')
def email(str_data):
mail_host = 'smtp.qq.cn'
mail_user = '******@qq.com' #邮箱账户
mail_pass = '*******' #邮箱授权码
sender = '******@qq.com' #发送邮箱账户
receivers = ['*****@qq.com,*****@qq.com'] #接受邮箱账户
mail_msg = """
<html>
<body>
<p><a href="https://www.jisilu.cn/data/cbnew/#pre">集思录转债</a></p>
$(data_list)
</body>
</html>
"""
mail_msg = mail_msg.replace("$(data_list)", str_data)
message = MIMEText(mail_msg, 'html', 'utf-8')
message['Subject'] = '转债申购/上市消息'
message['From'] = sender
# message['To'] = receivers[0]
# message['To'] = receivers
message['To'] = ','.join(receivers)
try:
# smtpObj = smtplib.SMTP()
# smtpObj.connect(mail_host, 25)
smtpObj = smtplib.SMTP_SSL("smtp.qq.com", 465)
smtpObj.set_debuglevel(1)
smtpObj.login(mail_user, mail_pass)
# smtpObj.sendmail(sender, receivers, message.as_string())
smtpObj.sendmail(sender, message['To'].split(','), message.as_string())
smtpObj.quit()
print('Email sent successfully')
except smtplib.SMTPException as e:
print('error', e)
if __name__ == '__main__':
web()
成果如下:
然后将脚本放到电脑上,设置每天定时运行。这里我用的是centos服务器的crontab定时服务。
编辑crontab脚本
crontab -e
添加如下代码,然后保存
46 09 * * 1,2,3,4,5 python3 /home/sky/main.py
每周1,2,3,4,5早上9点46定时运行此脚本(周末股市休息所以不运行)
重启crontab
systemctl restart crond