Python批量獲取基金數據的方法步驟
20年初準備投資基金,想爬取基金的業績數據。
20年基金迎來瞭爆發式增長,現把代碼開源以供參考。
本代碼隻能實現初步匯總,輸出csv文件來保存基金的單位&累計凈值,後期仍需要結合統計方法來篩選優質基金。
參考瞭網上的部分代碼,實在不記得出處瞭,侵刪。
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批量獲取基金數據的方法步驟的文章就介紹到這瞭,更多相關Python批量獲取基金數據內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!
推薦閱讀:
- 深入淺析Python數據分析的過程記錄
- Python接單的過程記錄分享
- python中response.text 和response.content的區別詳解
- Python腳本開發漏洞的批量搜索與利用(GlassFish 任意文件讀取)
- Python爬蟲之requests庫基本介紹