Python批量獲取基金數(shù)據(jù)的方法步驟
20年初準(zhǔn)備投資基金,想爬取基金的業(yè)績(jī)數(shù)據(jù)。
20年基金迎來了爆發(fā)式增長,現(xiàn)把代碼開源以供參考。
本代碼只能實(shí)現(xiàn)初步匯總,輸出csv文件來保存基金的單位&累計(jì)凈值,后期仍需要結(jié)合統(tǒng)計(jì)方法來篩選優(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)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python使用正則表達(dá)式實(shí)現(xiàn)文本替換的方法
這篇文章主要介紹了Python使用正則表達(dá)式實(shí)現(xiàn)文本替換的方法,結(jié)合實(shí)例形式分析了Python使用正則表達(dá)式實(shí)現(xiàn)文本替換的具體操作步驟與相關(guān)使用注意事項(xiàng),需要的朋友可以參考下2017-04-04Django使用Celery異步任務(wù)隊(duì)列的使用
這篇文章主要介紹了Django使用Celery異步任務(wù)隊(duì)列的使用,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-03-03Python基礎(chǔ)之函數(shù)嵌套知識(shí)總結(jié)
今天帶大家回顧python基礎(chǔ)知識(shí),文中對(duì)Python函數(shù)嵌套作了非常詳細(xì)的知識(shí)總結(jié),對(duì)正在學(xué)習(xí)python基礎(chǔ)的小伙伴們很有幫助,需要的朋友可以參考下2021-05-05Python Json模塊中dumps、loads、dump、load函數(shù)介紹
本篇文章主要介紹了Python Json模塊中dumps、loads、dump、load函數(shù)介紹,詳細(xì)的介紹了這幾種函數(shù)的用法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05Python新版極驗(yàn)驗(yàn)證碼識(shí)別驗(yàn)證碼教程詳解
這篇文章主要介紹了Python新版極驗(yàn)驗(yàn)證碼識(shí)別驗(yàn)證碼,極驗(yàn)驗(yàn)證是一種在計(jì)算機(jī)領(lǐng)域用于區(qū)分自然人和機(jī)器人的,通過簡(jiǎn)單集成的方式,為開發(fā)者提供安全、便捷的云端驗(yàn)證服務(wù)2023-02-02