20年初準(zhǔn)備投資基金,想爬取基金的業(yè)績(jī)數(shù)據(jù)。
20年基金迎來(lái)了爆發(fā)式增長(zhǎng),現(xiàn)把代碼開(kāi)源以供參考。
本代碼只能實(shí)現(xiàn)初步匯總,輸出csv文件來(lái)保存基金的單位累計(jì)凈值,后期仍需要結(jié)合統(tǒng)計(jì)方法來(lái)篩選優(yōu)質(zhì)基金。
參考了網(wǎng)上的部分代碼,實(shí)在不記得出處了,侵刪。
import requests
import time
import execjs
start = time.perf_counter()
# 獲取所有基金編號(hào)
def getAllCode():
url = 'http://fund.eastmoney.com/js/fundcode_search.js'
content = requests.get(url)
jsContent = execjs.compile(content.text)
rawData = jsContent.eval('r')
allCode = []
for code in rawData:
allCode.append(code[0])
return allCode
allCode = getAllCode()
del allCode[100:len(allCode)]
# print(len(allCode))
# 獲取基金編號(hào)為fscode的所有信息
def getUrl(fscode):
head = 'http://fund.eastmoney.com/pingzhongdata/'
tail = '.js?v=' + time.strftime("%Y%m%d%H%M%S", time.localtime())
return head + fscode + tail
# 獲取凈值
def getWorth(fscode):
content = requests.get(getUrl(fscode))
jsContent = execjs.compile(content.text)
name = jsContent.eval('fS_name')
code = jsContent.eval('fS_code')
# 單位凈值走勢(shì)
netWorthTrend = jsContent.eval('Data_netWorthTrend')
# 累計(jì)凈值走勢(shì)
ACWorthTrend = jsContent.eval('Data_ACWorthTrend')
# 近一年收益率
Profit_12month = jsContent.eval('syl_1n')
netWorth = []
ACWorth = []
for dayWorth in netWorthTrend[::-1]:
netWorth.append(dayWorth['y'])
for dayACWorth in ACWorthTrend[::-1]:
ACWorth.append(dayACWorth[1])
print(name, code)
return netWorth, ACWorth
netWorthFile = open('./netWorth.csv', 'w')
ACWorthFile = open('./ACWorth.csv', 'w')
for code in allCode:
try:
netWorth, ACWorth = getWorth(code)
except:
continue
if len(netWorth) = 0 or len(ACWorth) 0:
# print(code + " empty data")
continue
netWorthFile.write("\'" + code + "',")
netWorthFile.write(",".join(list(map(str, netWorth))))
netWorthFile.write("\n")
ACWorthFile.write("\'" + code + "',")
ACWorthFile.write(",".join(list(map(str, ACWorth))))
ACWorthFile.write("\n")
# print("write " + code + " success.")
netWorthFile.close()
ACWorthFile.close()
end = time.perf_counter()
print('Running time: %s seconds' %(end-start))
到此這篇關(guān)于Python批量獲取基金數(shù)據(jù)的方法步驟的文章就介紹到這了,更多相關(guān)Python批量獲取基金數(shù)據(jù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:- python 簡(jiǎn)單的股票基金爬蟲(chóng)
- Python獲取基金網(wǎng)站網(wǎng)頁(yè)內(nèi)容、使用BeautifulSoup庫(kù)分析html操作示例
- Python多進(jìn)程方式抓取基金網(wǎng)站內(nèi)容的方法分析
- Python學(xué)習(xí)筆記之抓取某只基金歷史凈值數(shù)據(jù)實(shí)戰(zhàn)案例
- 利用python實(shí)時(shí)刷新基金估值(摸魚(yú)小工具)