最近因为支付宝送了我个红包,可以买基金,又看了一些基金的文章,想买点基金玩玩,在知乎上看到这样一篇文章:
其实如果是长期定投,用简单的方法筛选即可:
晨星近5年、近3年评级为五星;
天天的业绩排名近3年、2年、1年、6个月的业绩都为优秀;
基金经理人在近3年无更换。
然后我就想及时知道我买的基金的经理人有没有换,并发邮件来用通知我。所以有了下面这段程序
import requests
from bs4 import BeautifulSoup
url = 'http://fund.eastmoney.com/000311.html?spm=search'
request = requests.get(url)
request.encoding = 'utf-8' #有时会乱码
result = request.text
html = BeautifulSoup(result,"lxml") #解析
titel = html.select_one("#body div.fundDetail-main > div.fundInfoItem > div.infoOfFund td:nth-of-type(3) a") #select返回的是一个列表
name = titel.get_text().strip()
print(name)
if name != "黎海威":
import smtplib
from email.mime.text import MIMEText
from email.header import Header
# 三个参数:第一个为文本内容,第二个 plain 设置文本格式,第三个 utf-8 设置编码
msg = MIMEText('景顺长城沪深300增强(000311):基金经理有变动,已不是黎海威!请查收。', 'plain', 'utf-8')
# 输入Email地址和口令:
from_addr = 'XXX@163.com'
password = 'XXX'
# 输入SMTP服务器地址:
smtp_server = 'smtp.163.com'
# 输入收件人地址:
to_addr = 'XXX@qq.com,XXX@qq.com'
msg['From'] = from_addr
msg['To'] = to_addr
msg['Subject'] = '基金经理变更'
server = smtplib.SMTP(smtp_server, 25) # SMTP协议默认端口是25
server.set_debuglevel(1)
server.login(from_addr, password)
server.sendmail(from_addr, to_addr.split(','), msg.as_string()) ##发给多个人
server.quit()
第二版:爬取多个基金
#====================================第二版=========================================
import requests
from bs4 import BeautifulSoup
#邮件
import smtplib
from email.mime.text import MIMEText
from email.header import Header
names_pc=[] #爬虫下来的名字
fundnames = ['景顺长城沪深300增强(000311)','兴全沪深300指数(LOF)(163407)']
names_pass = ['黎海威', '申庆']
stock_code = ['000311','163407']
urls =[]
for i in stock_code:
url = 'http://fund.eastmoney.com/'+str(i)+'.html?spm=search'
urls.append(url)
for url in urls:
request = requests.get(url)
request.encoding = 'utf-8' #有时会乱码
result = request.text
html = BeautifulSoup(result,"lxml") #解析
titel = html.select_one("#body div.fundDetail-main > div.fundInfoItem > div.infoOfFund td:nth-of-type(3) a") #select返回的是一个列表,用select的话用[0]可以
name = titel.get_text().strip()
print(name)
names_pc.append(name)
print(names_pc)
from_addr = 'XXXX@163.com'
password = 'XXXX'
smtp_server = 'smtp.163.com' # 输入SMTP服务器地址:
to_addr = 'XXX@qq.com,XXX@qq.com' # 输入收件人地址:
for name_pc,name_pass,fundname in zip(names_pc,names_pass,fundnames):
if name_pc != name_pass:
text = '{}:基金经理有变动,已不是{}!请查收'.format(fundname,name_pass)
msg = MIMEText(text, 'plain', 'utf-8')
msg['From'] = from_addr
msg['To'] = to_addr
msg['Subject'] = '基金经理变更' ##主题
server = smtplib.SMTP(smtp_server, 25) # SMTP协议默认端口是25
server.set_debuglevel(1)
server.login(from_addr, password)
server.sendmail(from_addr, to_addr.split(','), msg.as_string()) ##发给多个人
server.quit()
定时任务
既然已经写好程序了,当然希望它每天能跑,然后我就去阿里云租了台服务器
输入crontab -e
来执行定时任务,注意上面的程序在阿里云上需要将smtplib.SMTP
函数改成smtplib.SMTP_SSL来执行才能跑通
0 10 * * * /usr/bin/python3 /home/admin/fund/fundchange2email_v1.py > /home/admin/fund/fund.log
再ctrl+x
保存并退出