财务的自我修养,微信公众号:机智出品(jizhjchupin),文中各类数据文件均可于公众号内下载
现在整个市场上有近7000支基金产品,股票型、混合型、债券型、QDII、ETF、LOF等各种类型。近一年有涨了50%,也有跌幅超过30%的。面对这么多基金,我们该如何选出适合自己的赚钱的基金?
本次介绍数据获取过程,请看下文......(对获取数据过程无兴趣者,可直接下载数据)
相关文章:懒人必备神器Python获取股票基金行情并发邮箱提醒
一、获取基金列表:
下面我们用Python爬取全场所有基金的代码名称等。文件下载:fundlist.xlsx,密码公众号内回复fundlist可得。
# -*- coding: utf-8 -*-
"""
Created on Thu Jul 13 19:34:44 2017
@author: le
"""
import xlsxwriter
import requests
import time
def openurl(url):
try:
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36'}
r = requests.get(url,headers = headers)
r.raise_for_status()
r.encoding = r.apparent_encoding
return r.text
except Exception as e:
print('Error:',e)
def writexls(fundlist):
#name=values[1]
timenow=time.strftime("%Y-%m-%d", time.localtime())
filename=timenow+'基金列表.xlsx'
workbook = xlsxwriter.Workbook(filename)
#第一页sheet
worksheet1 = workbook.add_worksheet("基金信息")
worksheet1.set_column('B:B', 16)
worksheet1.set_column('C:C', 12)
worksheet1.set_column('F:F', 14)
worksheet1.set_column('G:G', 16)
titleFormat = workbook.add_format()
titleFormat.set_bold()
titleFormat.set_bg_color("#4F81BD")
titleFormat.set_font_size(12)
titleFormat.set_align("center")
#titleFormat.set_align("vcenter")
titleFormat.set_border(1)
titleFormat.set_font_color('white')
contentFormat = workbook.add_format()
contentFormat.set_align("center")
#contentFormat.set_align("vcenter")
titleFormat.set_border(1)
titleFormat.set_font_color('white')
headList = ["基金代码",
"基金简码",
"基金名称",
"基金类型",
"基金字母"]
for i in range(len(headList)):
worksheet1.write(0, i, headList[i], titleFormat)
for n in range(len(fundlist)):
for i in range(len(fundlist[n])):
try:
worksheet1.write(n+1, i, fundlist[n][i], contentFormat)
except:
pass
workbook.close()
url='http://fund.eastmoney.com/js/fundcode_search.js'
html=openurl(url)
fundlist=eval(html[8:-1])
print(fundlist[0])
for i in fundlist[0]:
print(i)
print(len(fundlist[0]))
writexls(fundlist)
二、获取6000多基金近两年数据:
用刚才获得的基金代码列表爬取历史数据,并存到MySQL里面。有差不多251万多条数据。全部基金近两年行情数据CSV文件下载:fund.zip,密码公众号内回复fundzip可得。
导出CSV.png# -*- coding: utf-8 -*-
"""
Created on Sat Oct 21 22:37:03 2017
@author: le
"""
import pandas as pd
from sqlalchemy import create_engine
import time
def save_sql(code):
df=pd.read_html('http://fund.eastmoney.com/f10/F10DataApi.aspx?type=lsjz&code='+code+'&page=1&per=730')
df=df[0]
df.columns =['date','nav','sum_nav','ret','a','b','c']
del df['a']
del df['b']
del df['c']
engine = create_engine('mysql+pymysql://root:password@127.0.0.1:3306/test?charset=utf8')
df.to_sql(code, engine)
def main():
df=pd.read_excel('C:\\Users\\le\\Desktop\\fundlist.xlsx',converters = {u'基金代码':str})
codes=df['基金代码']
i=0
for code in codes:
print(i,code)
try:
save_sql(code)
except:
#6000多基金两年的数据,爬的过程中经常会被封。。。。。
print('error',code)
with open('error.txt','a') as f:
f.write(i+','+code+'\n')
f.close()
time.sleep(60)#休息休息吧,虽然我一鼓作气不怕死
if __name__ == "__main__":
main()
三、251万基金数据分析,及选基策略:
未完待续......(挖个坑下次写)
希望解决下列问题吧
1、各类型基金收益排名
2、基金投资策略
3、定投选星期几收益更高?
4、一个月买一次还是一个星期买一次好?
5、基金该不该止盈?
机智出品.jpg
网友评论