財(cái)務(wù)的自我修養(yǎng),微信公眾號(hào):機(jī)智出品(jizhjchupin)碉考,文中各類數(shù)據(jù)文件均可于公眾號(hào)內(nèi)下載
現(xiàn)在整個(gè)市場(chǎng)上有近7000支基金產(chǎn)品,股票型挺身、混合型侯谁、債券型、QDII章钾、ETF墙贱、LOF等各種類型。近一年有漲了50%贱傀,也有跌幅超過(guò)30%的惨撇。面對(duì)這么多基金,我們?cè)撊绾芜x出適合自己的賺錢的基金窍箍?
本次介紹數(shù)據(jù)獲取過(guò)程串纺,請(qǐng)看下文......(對(duì)獲取數(shù)據(jù)過(guò)程無(wú)興趣者丽旅,可直接下載數(shù)據(jù))
相關(guān)文章:懶人必備神器Python獲取股票基金行情并發(fā)郵箱提醒
一、獲取基金列表:
下面我們用Python爬取全場(chǎng)所有基金的代碼名稱等纺棺。文件下載:fundlist.xlsx榄笙,密碼公眾號(hào)內(nèi)回復(fù)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)
#第一頁(yè)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 = ["基金代碼",
"基金簡(jiǎn)碼",
"基金名稱",
"基金類型",
"基金字母"]
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多基金近兩年數(shù)據(jù):
用剛才獲得的基金代碼列表爬取歷史數(shù)據(jù)茅撞,并存到MySQL里面。有差不多251萬(wàn)多條數(shù)據(jù)巨朦。全部基金近兩年行情數(shù)據(jù)CSV文件下載:fund.zip米丘,密碼公眾號(hào)內(nèi)回復(fù)fundzip可得。
# -*- 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多基金兩年的數(shù)據(jù)糊啡,爬的過(guò)程中經(jīng)常會(huì)被封拄查。。棚蓄。堕扶。。
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萬(wàn)基金數(shù)據(jù)分析,及選基策略:
未完待續(xù)......(挖個(gè)坑下次寫)
希望解決下列問(wèn)題吧
1役拴、各類型基金收益排名
2糊探、基金投資策略
3、定投選星期幾收益更高河闰?
4科平、一個(gè)月買一次還是一個(gè)星期買一次好?
5淤击、基金該不該止盈匠抗?