本篇文章給大家分享的是有關(guān)使用Python怎么批量獲取基金數(shù)據(jù),小編覺得挺實用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
Python是一種編程語言,內(nèi)置了許多有效的工具,Python幾乎無所不能,該語言通俗易懂、容易入門、功能強大,在許多領(lǐng)域中都有廣泛的應(yīng)用,例如最熱門的大數(shù)據(jù)分析,人工智能,Web開發(fā)等。
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))
以上就是使用Python怎么批量獲取基金數(shù)據(jù),小編相信有部分知識點可能是我們?nèi)粘9ぷ鲿姷交蛴玫降?。希望你能通過這篇文章學(xué)到更多知識。更多詳情敬請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。