1.获取东方财富研报列表中属于超预期的研报
2.邮件发送
import requests
import re
import json
import smtplib
from jinja2 import Environment, FileSystemLoader
from email.mime.text import MIMEText
from email.utils import formataddr
from retrying import retry
import schedule
import time
import tushare as ts
SendedList = [] # 已发送列表
# base params
base_url = 'http://datainterface.eastmoney.com//EM_DataCenter/js.aspx'
base_cookie = 'emstat_bc_emcount=37889085062188945262; st_pvi=34681393574450; _ga=GA1.2.1970903645.1486349341; HAList=a-sz-002362-%u6C49%u738B%u79D1%u6280%2Ca-sz-000807-%u4E91%u94DD%u80A1%u4EFD%2Ca-sz-300288-%u6717%u739B%u4FE1%u606F%2Ca-sz-300466-%u8D5B%u6469%u7535%u6C14%2Ca-sh-600996-%u8D35%u5E7F%u7F51%u7EDC%2Ca-sz-000713-%u4E30%u4E50%u79CD%u4E1A; em_hq_fls=old; emstat_ss_emcount=3_1511216938_603191144; qgqp_b_id=5033d538a451b91de2b58f645b83b1f6; st_si=81586899525171'
base_headers = {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
'Accept - Encoding': 'gzip, deflate',
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
'Cache-Control': 'max-age=0',
'Connection': 'Keep-Alive',
'Host': 'datainterface.eastmoney.com',
'Upgrade-Insecure-Requests': '1',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36'
}
def getstockgains(stockcode, start, end):
df = ts.get_hist_data(stockcode, start, end)
return df
# 邮件发送
@retry
def mail(msgcontent):
ret = True
try:
msg = MIMEText(msgcontent, 'html', 'utf-8')
# 括号里的对应发件人邮箱昵称、发件人邮箱账号
msg['From'] = formataddr(["From平凡啊菜", my_sender])
# 括号里的对应收件人邮箱昵称、收件人邮箱账号
msg['To'] = formataddr(["平凡啊菜", recipients[0]])
msg['Subject'] = "研报超预期列表" # 邮件的主题,也可以说是标题
server = smtplib.SMTP_SSL("smtp.qq.com", 465) # 发件人邮箱中的SMTP服务器,端口是25
server.login(my_sender, my_pass) # 括号中对应的是发件人邮箱账号、邮箱密码
# 括号中对应的是发件人邮箱账号、收件人邮箱账号、发送邮件
server.sendmail(
my_sender, recipients[0], msg.as_string())
server.quit()
except Exception:
ret = False
return ret
@retry
def getReportData(mp):
base_params = {'type': 'SR', 'sty': 'GGSR',
'js': 'var allResult={"data":[(x)],"pages":"(pc)","update":"(ud)","count":"(count)"}', 'ps': 50,
'p': mp, 'mkt': 0, 'stat': 0, 'cmd': 2, 'code': '', 'rt': 50436030}
s = requests.Session()
rval = s.get(base_url, params=base_params, timeout=300)
# print(rval.text)
if rval.status_code == 200:
# print('请求结果:%s' % rval.text)
match = re.search(r'allResult={(.+)}', rval.text)
# print('match:%s' % match)
reportinfo = ''
if match:
reportinfo = str(match.group(1))
# print('reportinfo:%s' % reportinfo)
reportinfo = '{' + reportinfo + '}'
# print(reportinfo)
decodejson = json.loads(reportinfo)
# print('json:%s' % decodejson)
return decodejson
def filterbestreports():
# print('do filter best reports job.')
filterlist = []
pages = 2
mindex = 1
while mindex <= pages:
reports = getReportData(mindex)
pages = int(reports['pages'])
for item in reports['data']:
# print(item)
if str(item['title']).find('超预期') != -1:
if item in SendedList:
# print('sendedlist find:%s' % item)
continue
filterlist.append(item)
print('代码:%s,名称:%s,研报:%s,机构:%s,日期:%s' % (item['secuFullCode'], item[
'secuName'], item['title'], item['insName'], item['datetime']))
mindex = mindex + 1
# print('搜索到第%s/%s页' % (mindex, pages))
if len(filterlist) != 0:
env = Environment(loader=FileSystemLoader('template'))
email_temp = env.get_template('eastmoney_reports.html')
html = email_temp.render(results=filterlist)
success = mail(html)
if success:
SendedList.extend(filterlist)
def main():
filterbestreports()
schedule.every(1800).seconds.do(filterbestreports)
while True:
schedule.run_pending()
time.sleep(1)
if __name__ == '__main__':
my_sender = 'xxxx@qq.com' # 发件人邮箱账号
my_pass = '' # 发件人邮箱授权码
# 收件人邮箱账号
recipients = ['xxx@qq.com', 'xxxx@qq.com']
main()
强烈推荐文章:
2020年4月至6月净利润断层实战小结-真香~