您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關使用Python怎么批量獲取基金數據,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
Python是一種編程語言,內置了許多有效的工具,Python幾乎無所不能,該語言通俗易懂、容易入門、功能強大,在許多領域中都有廣泛的應用,例如最熱門的大數據分析,人工智能,Web開發等。
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怎么批量獲取基金數據,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。