Python批量獲取基金數(shù)據(jù)的方法步驟
20年初準(zhǔn)備投資基金,想爬取基金的業(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)文章
pycharm安裝django框架詳細(xì)圖文教程(指定版本)
這篇文章主要給大家介紹了關(guān)于pycharm安裝django框架(指定版本)的相關(guān)資料,PyCharm是一種Python?IDE,帶有一整套可以幫助用戶在使用Python語言開發(fā)時提高其效率的工具,需要的朋友可以參考下2023-10-10
python使用tkinter調(diào)整label背景顏色的測試
這篇文章主要介紹了python使用tkinter調(diào)整label背景顏色的測試方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-09-09
修復(fù) Django migration 時遇到的問題解決
本篇文章主要介紹了修復(fù) Django migration 時遇到的問題解決,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-06-06
Python實現(xiàn)動態(tài)圖解析、合成與倒放
這篇文章主要為大家詳細(xì)介紹了Python實現(xiàn)動態(tài)圖的解析、合成與倒放,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-01-01
Pytorch深度學(xué)習(xí)之實現(xiàn)病蟲害圖像分類
PyTorch是一個開源的Python機(jī)器學(xué)習(xí)庫,基于Torch,用于自然語言處理等應(yīng)用程序。它具有強(qiáng)大的GPU加速的張量計算和自動求導(dǎo)系統(tǒng)的深度神經(jīng)網(wǎng)絡(luò)。本文將介紹如何通過PyTorch實現(xiàn)病蟲害圖像分類,感興趣的可以學(xué)習(xí)一下2021-12-12
python+Tesseract OCR實現(xiàn)截屏識別文字
pytesseract Python常用pytesseract進(jìn)行圖片上的文字識別,本文主要介紹了python+Tesseract?OCR實現(xiàn)截屏識別文字,具有一定的參考價值,感興趣的可以了解一下2023-11-11
Python 如何將integer轉(zhuǎn)化為羅馬數(shù)(3999以內(nèi))
這篇文章主要介紹了Python 將integer轉(zhuǎn)化為羅馬數(shù)(3999以內(nèi))的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06

