Python批量獲取基金數(shù)據(jù)的方法步驟
20年初準備投資基金,想爬取基金的業(yè)績數(shù)據(jù)。
20年基金迎來了爆發(fā)式增長,現(xiàn)把代碼開源以供參考。
本代碼只能實現(xiàn)初步匯總,輸出csv文件來保存基金的單位&累計凈值,后期仍需要結(jié)合統(tǒng)計方法來篩選優(yōu)質(zhì)基金。
參考了網(wǎng)上的部分代碼,實在不記得出處了,侵刪。
import requests import time import execjs start = time.perf_counter() # 獲取所有基金編號 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)) # 獲取基金編號為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') # 單位凈值走勢 netWorthTrend = jsContent.eval('Data_netWorthTrend') # 累計凈值走勢 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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python基礎(chǔ)之函數(shù)嵌套知識總結(jié)
今天帶大家回顧python基礎(chǔ)知識,文中對Python函數(shù)嵌套作了非常詳細的知識總結(jié),對正在學(xué)習(xí)python基礎(chǔ)的小伙伴們很有幫助,需要的朋友可以參考下2021-05-05Python Json模塊中dumps、loads、dump、load函數(shù)介紹
本篇文章主要介紹了Python Json模塊中dumps、loads、dump、load函數(shù)介紹,詳細的介紹了這幾種函數(shù)的用法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-05-05